Get Adobe Flash player

Generate SQL Server Index Creates

There are several reasons you may want to create script to add indexes. If you have used SQL Server DTS application to transfer data from one database to another, the data will be transferred over, but not the indexes. If you used the enterprise manager (or management studio) to script a table create, indexes again will not be created. Want a brute force way of recreating all of your indexes? Read further.

Script GenerateIndexes.sql is an easy way to create indexes. It will look at all user defined tables and create an output that can be used to create indexes. How to use GenerateIndexes.sql:

  1. copy or open GenerateIndexes.sql in a query tool: Query Analyzer, or Management Studio.
  2. switch to the desired database
  3. execute script.
  4. Copy the output to a file or another query window then execute.
  5. Other Uses: Want to drop all of the indexed prior to creating them? Change one line of code from Select @DropIndex = 0 to Select @DropIndex = 1 2) Don't want all tables? Around line 43, look for --And sysobjects.Name in ('Table1'). Remove the line comment (--) and Replace with a list of tables you need.