Subscribe | Alerts via Email
View All Quotes
“The greatest challenge to any thinker is stating the problem in a way that will allow a solution.”
-Bertrand Russell
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

©2010 Cal Zant
Sign In
Total Posts: 106
This Year: 5
This Month: 1
This Week: 0
Comments: 2

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)  #