Subscribe | Alerts via Email
View All Quotes
“Imagination is more important than knowledge. For knowledge is limited, whereas imagination embraces the entire world, stimulating progress, giving birth to evolution.”
-Albert Einstein
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

©2012 Cal Zant
Sign In
Total Posts: 107
This Year: 0
This Month: 0
This Week: 0
Comments: 0

In my recent post on How To: Find Missing Indexes in SQL Server, I showed how to find the information related to indexes that SQL Server thinks would be helpful (based on query stats it has been keeping since it was last rebooted).  The actual "CREATE INDEX" script you have to write based on that info can be a little tedious to get write, so I created a user-defined function that you could simply pass some of the info you get from the query in that post and it will return the necessary T-SQL you would need to run in order to create the index for the given info.

ALTER FUNCTION [dbo].[bcz_fn_GetCreateIndexScript](@TableName varchar(100), @EqualityColumns varchar(500), @InequalityColumns varchar(500), @IncludedColumns varchar(500))
RETURNS varchar(MAX)
BEGIN

 DECLARE @CreateScript varchar(MAX), @Name varchar(128), @EqualityAndInequalityColumnList varchar(MAX), @ColumnListForName varchar(MAX), @MaxColumnListLength int, @IndexPrefix varchar(63)

 -- Construct what the prefix of the index will be (this will be followed by the list of related columns)
 SET @IndexPrefix = 'IX_' + @TableName + '_'

 -- Construct a list of all columns that will be involved with the index (equality, inequality, and included)
 SET @EqualityAndInequalityColumnList = ''
 IF @EqualityColumns IS NOT NULL
  SET @EqualityAndInequalityColumnList = @EqualityColumns
 
 IF @InequalityColumns IS NOT NULL
 BEGIN
  IF (LEN(@EqualityAndInequalityColumnList) > 0) SET @EqualityAndInequalityColumnList = @EqualityAndInequalityColumnList + ', ' + @InequalityColumns
  ELSE SET @EqualityAndInequalityColumnList = @InequalityColumns
 END
 
 SET @ColumnListForName = @EqualityAndInequalityColumnList
 
 IF @IncludedColumns IS NOT NULL
 BEGIN
  IF (LEN(@ColumnListForName) > 0) SET @ColumnListForName = @ColumnListForName + ', ' + @IncludedColumns
  ELSE SET @ColumnListForName = @IncludedColumns
 END

 -- Remove the brackets around the column names, and make it "_" delimited instead of ", " delimited
 SET @ColumnListForName = REPLACE(REPLACE(REPLACE(@ColumnListForName, '[', ''), ']', ''), ', ', '_')
 
 -- The max length of the index name must be <= 128 ... so calculate how big the column list can be with
 -- respect to that and the given naming convention we will use.
 SET @MaxColumnListLength = 128 - LEN(@IndexPrefix)

 -- Create the name with the list of columns appended to the end of it
 SET @Name = @IndexPrefix + SUBSTRING(@ColumnListForName, 1, @MaxColumnListLength)

 SET @CreateScript =
  'CREATE NONCLUSTERED INDEX ' + @Name + ' ON ' + @TableName + ' (' + @EqualityAndInequalityColumnList + ')'
  
 IF @IncludedColumns IS NOT NULL
  SET @CreateScript = @CreateScript + ' INCLUDE (' + @IncludedColumns + ')'

 RETURN @CreateScript
END

If you would like to dig deeper into this, here is a good resource: http://msdn.microsoft.com/en-us/library/ms345405.aspx.

Wednesday, July 08, 2009 1:29:35 PM (Central Standard Time, UTC-06:00)  #