Generating Index scripts for SQL Server 2005 in Excel

So, I go further in my series of blogs on automating SQL Server 2005 scripting. This time its the tool of the performance sensitive DBA: the nonclustered index. These are pretty straightforward and should be easy to implement.  The first formula is there to delete a previous version of the index should it exist.  In your Excel sheet I have the table name in column A and B has the field or column name. Here are the formulae for columns D and E (I leave column C for notes):

=”if exists (select * from sys.indexes where name=N’index_”&A4&”_”&B4&”‘) drop index “&A4&”.index_”&A4&”_”&B4&”; “

=”create nonclustered index [index_"&A4&"_"&B4&"] on [dbo].["&A4&"](["&B4&"] asc) with (pad_index = off, statistics_norecompute = off, sort_in_tempdb = off, ignore_dup_key = off, online = off, allow_row_locks = on, allow_page_locks = on) on [primary];”

with “tbltest” and “foreignid_fk” in the A and B cells I get the following results:

if exists (select * from sys.indexes where name=N’index_tbltest_foreignid_fk’) drop index tbltest.index_tbltest_foreignid_fk;

create nonclustered index [index_tbltest_foreignid_fk] on [dbo].[tbltest]([foreignid_fk] asc) with (pad_index = off, statistics_norecompute = off, sort_in_tempdb = off, ignore_dup_key = off, online = off, allow_row_locks = on, allow_page_locks = on) on [primary];

Note that I intentionally leave out the “drop_existing” clause in the index create. It can lead to errors and doesn’t seem to be very useful.

2 Responses to “Generating Index scripts for SQL Server 2005 in Excel”

  1. fact interesting Says:

    The main thing i’m enjoying while reading your blog is the way you write, you are a really charismatic person and your posts are wonderful, keep it up!

  2. Improved Index Scripting from Excel « Golbing Says:

    [...] 2008-06-2 Posted by tgrignon in Miscellany. trackback I covered Index Scripts using Excel in an earlier post but I was obliged to make my spreadsheet do more over the past few workdays. Basically I needed to [...]

Leave a Reply