I have updated my previously posted query to include more index types and in doing so, split the query into two (one for SQL Server 2012 and earlier and one for SQL Server 2014 and later). Again, thank you for writing this post!
SQL Server 2014 and later:
/*
This script will generate the SQL to create all indexes in the database of SQL Server 2014 and greater.
Copied from https://www.mssqltips.com/sqlservertip/3441/script-out-all-sql-server-indexes-in-a-database-using-tsql/.
Modified by GaryS 4-14-2016.
Modified to add ALTER TABLE for PK indexes and unique constraints, data compression, filtered indexes, XML indexes,
partitioned indexes, spatial indexes, indexes on memory-optimized tables (including hash indexes), and online options (when applicable).
NOTE: Does not script out Extended Properties (comments), or full-text indexes (and catalogs).
*/
DECLARE @SchemaName varchar(100);
DECLARE @TableName varchar(256);
DECLARE @IndexName varchar(256);
DECLARE @ColumnName varchar(100);
DECLARE @is_unique varchar(100);
DECLARE @IndexTypeDesc varchar(100);
DECLARE @FileGroupName varchar(100);
DECLARE @is_disabled varchar(100);
DECLARE @IndexOptions varchar(MAX);
DECLARE @DataCompressionType varchar(100);
DECLARE @is_filtered bit;
DECLARE @filter_definition varchar(MAX);
DECLARE @is_primary_key bit;
DECLARE @is_unique_constraint bit;
DECLARE @IndexColumnId int;
DECLARE @IsDescendingKey int;
DECLARE @IsIncludedColumn int;
DECLARE @compression_delay int;
DECLARE @TSQLScripCreationIndex varchar(MAX);
DECLARE @TSQLScripDisableIndex varchar(MAX);
DECLARE CursorIndex
CURSOR
FOR
--CTE to collect partitioned index information
WITH PartitionedIndexes AS (
SELECT t.object_id Object_ID,
t.name TableName,
ic.column_id PartitioningColumnID,
c.name PartitioningColumnName,
s.name AS [partition_scheme],
ix.name AS IndexName,
ix.index_id
FROM sys.tables t
INNER JOIN sys.indexes i ON i.object_id = t.object_id
INNER JOIN sys.index_columns ic ON ic.index_id = i.index_id
AND ic.object_id = t.object_id
INNER JOIN sys.columns c ON c.object_id = ic.object_id
AND c.column_id = ic.column_id
INNER JOIN sys.partition_schemes s ON i.data_space_id = s.data_space_id
INNER JOIN sys.indexes ix ON t.object_id=ix.object_id
AND ix.index_id = i.index_id
WHERE ic.partition_ordinal = 1
)
SELECT schema_name(t.schema_id) [schema_name],
t.name TableName,
ix.name IndexName,
CASE
WHEN ix.is_unique = 1 THEN 'UNIQUE '
ELSE ''
END IsUnique,
CASE
WHEN t.object_id IN (SELECT OBJECT_ID FROM sys.tables WHERE is_memory_optimized = 1)
AND ix.type_desc <> 'NONCLUSTERED HASH'
THEN 'MEMORY_OPTIMIZED'
ELSE ix.type_desc
END AS
type_desc,
CASE
WHEN ix.is_padded=1 THEN 'PAD_INDEX = ON, '
ELSE 'PAD_INDEX = OFF, '
END +
CASE
WHEN INDEXPROPERTY(t.object_id, ix.name, 'IsStatistics') = 1 THEN 'STATISTICS_NORECOMPUTE = ON, '
ELSE 'STATISTICS_NORECOMPUTE = OFF, '
END +
CASE
WHEN (ix.is_primary_key = 1 OR ix.is_unique_constraint = 1) THEN ''
ELSE 'SORT_IN_TEMPDB = OFF, '
END +
CASE
WHEN ix.ignore_dup_key=1 THEN 'IGNORE_DUP_KEY = ON, '
ELSE 'IGNORE_DUP_KEY = OFF, '
END +
CASE
WHEN (@@Version LIKE '%ENTERPRISE%' AND (LOBTable.CanBeBuiltOnline IS NULL AND ix.type_desc = 'CLUSTERED')
AND ix.is_primary_key = 0 AND ix.is_unique_constraint = 0)
OR (@@Version LIKE '%ENTERPRISE%' AND (LOBIndexes.CanBeBuiltOnline IS NULL AND ix.type_desc <> 'CLUSTERED')
AND ix.is_primary_key = 0 AND ix.is_unique_constraint = 0)
THEN 'ONLINE = ON, '
ELSE ''
END +
CASE
WHEN ix.allow_row_locks=1 THEN 'ALLOW_ROW_LOCKS = ON, '
ELSE 'ALLOW_ROW_LOCKS = OFF, '
END +
CASE
WHEN ix.allow_page_locks=1 THEN 'ALLOW_PAGE_LOCKS = ON, '
ELSE 'ALLOW_PAGE_LOCKS = OFF, '
END +
CASE
WHEN CAST(ix.fill_factor AS VARCHAR(3)) = 0 THEN ''
ELSE 'FILLFACTOR =' + CAST(ix.fill_factor AS VARCHAR(3)) + ', '
END +
CASE
WHEN p.data_compression_desc IS NULL THEN 'DATA_COMPRESSION = NONE'
ELSE 'DATA_COMPRESSION = ' + p.data_compression_desc
END
AS IndexOptions,
ix.is_disabled,
CASE
WHEN ic.IsColumnPartitioned = 1 THEN '[' + PIdx.[partition_scheme] + ']' + '(' + '[' + PIdx.PartitioningColumnName + ']' + ')'
WHEN ic.IsColumnPartitioned = 0 THEN '[' + FILEGROUP_NAME(ix.data_space_id) + ']'
END AS FileGroupName,
ix.has_filter,
ix.filter_definition,
ix.is_primary_key,
ix.is_unique_constraint,
ix.compression_delay
FROM sys.tables t
INNER JOIN sys.indexes ix ON t.object_id = ix.object_id
INNER JOIN
(SELECT DISTINCT OBJECT_ID,
index_id,
MAX(partition_ordinal) AS IsColumnPartitioned
FROM sys.index_columns
GROUP BY OBJECT_ID,
index_id) ic ON ic.index_id = ix.index_id
AND ic.object_id = t.object_id
LEFT JOIN
(SELECT DISTINCT object_id,
index_id,
data_compression_desc
FROM sys.partitions) p ON ix.object_id = p.object_id
AND ix.index_id = p.index_id
LEFT JOIN PartitionedIndexes PIdx ON PIdx.Object_ID = t.object_id
AND PIdx.index_id = ix.index_id
LEFT JOIN
(SELECT DISTINCT object_id,
0 AS CanBeBuiltOnline
FROM sys.columns c
INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE t.Name IN ('image',
'ntext',
'text',
'XML')
OR (t.Name IN ('varchar',
'nvarchar',
'varbinary')
AND c.max_length = -1)
OR c.is_filestream = 1) LOBTable ON LOBTable.object_id = t.object_id
LEFT JOIN
(SELECT DISTINCT c.object_id,
i.index_id,
0 AS CanBeBuiltOnline
FROM sys.columns c
INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN sys.index_columns ic ON ic.object_id = c.object_id
AND ic.column_id = c.column_id
INNER JOIN sys.indexes i ON ic.object_id = i.object_id
AND ic.index_id = i.index_id
WHERE t.Name IN ('image',
'ntext',
'text',
'XML')
OR (t.Name IN ('varchar',
'nvarchar',
'varbinary')
AND c.max_length = -1)
OR c.is_filestream = 1) LOBIndexes ON LOBIndexes.object_id = t.object_id
AND LOBIndexes.index_id = ix.index_id
WHERE
/*****************Ignores PK indexes************************/
--ix.type > 0 AND ix.is_primary_key = 0 AND ix.is_unique_constraint = 0 AND
/***********************************************************/
t.is_ms_shipped = 0
AND t.name <> 'sysdiagrams'
AND ix.name IS NOT NULL
/*****************Ignores clustered indexes*****************/
--AND ix.type_desc <> 'CLUSTERED'
/***********************************************************/
/*****************Ignores COLUMNSTORE indexes***************/
--AND ix.type_desc NOT LIKE '%COLUMNSTORE%'
/***********************************************************/
ORDER BY schema_name(t.schema_id),
t.name,
ix.name
OPEN CursorIndex
FETCH NEXT
FROM CursorIndex
INTO @SchemaName,
@TableName,
@IndexName,
@is_unique,
@IndexTypeDesc,
@IndexOptions,
@is_disabled,
@FileGroupName,
@is_filtered,
@filter_definition,
@is_primary_key,
@is_unique_constraint,
@compression_delay
WHILE (@@fetch_status = 0)
BEGIN
DECLARE @IndexColumns varchar(MAX);
DECLARE @IncludedColumns varchar(MAX);
SET @IndexColumns='';
SET @IncludedColumns='';
SET @DataCompressionType = '';
DECLARE CursorIndexColumn
CURSOR
FOR
SELECT col.name,
ixc.is_descending_key,
ixc.is_included_column
FROM sys.tables tb
INNER JOIN sys.indexes ix ON tb.object_id = ix.object_id
INNER JOIN sys.index_columns ixc ON ix.object_id = ixc.object_id
AND ix.index_id = ixc.index_id
INNER JOIN sys.columns col ON ixc.object_id = col.object_id
AND ixc.column_id = col.column_id
WHERE
/*****************Ignores PK indexes************************/
--ix.type > 0 and (ix.is_primary_key = 0 or ix.is_unique_constraint = 0) AND
/***********************************************************/
schema_name(tb.schema_id) = @SchemaName
AND tb.name = @TableName
AND ix.name = @IndexName
/*****************Ignores clustered indexes*****************/
--AND ix.type_desc <> 'CLUSTERED'
/***********************************************************/
/*****************Ignores COLUMNSTORE indexes***************/
--AND ix.type_desc NOT LIKE '%COLUMNSTORE%'
/***********************************************************/
ORDER BY ixc.key_ordinal
OPEN CursorIndexColumn
FETCH NEXT
FROM CursorIndexColumn
INTO @ColumnName,
@IsDescendingKey,
@IsIncludedColumn
WHILE (@@fetch_status=0)
BEGIN
IF @IsIncludedColumn = 0
OR @IndexTypeDesc LIKE '%COLUMNSTORE%' OR @IndexTypeDesc = 'XML' OR @IndexTypeDesc = 'spatial'
OR @IndexTypeDesc = 'NONCLUSTERED HASH'
SET @IndexColumns =
CASE WHEN @IndexTypeDesc LIKE '%COLUMNSTORE%'
THEN @IndexColumns + '[' + @ColumnName + '], '
WHEN @IndexTypeDesc = 'XML' OR @IndexTypeDesc = 'spatial' OR @IndexTypeDesc = 'NONCLUSTERED HASH'
THEN @IndexColumns + '[' + @ColumnName + '], '
ELSE @IndexColumns + '[' + @ColumnName + ']' +
CASE
WHEN @IsDescendingKey = 1 THEN ' DESC, '
ELSE ' ASC, '
END
END
ELSE
SET @IncludedColumns = @IncludedColumns + '[' + @ColumnName + '], '
FETCH NEXT
FROM CursorIndexColumn
INTO @ColumnName,
@IsDescendingKey,
@IsIncludedColumn
END
CLOSE CursorIndexColumn
DEALLOCATE CursorIndexColumn
SET @IndexColumns = substring(@IndexColumns, 1, len(@IndexColumns)-1);
SET @IncludedColumns =
CASE
WHEN len(@IncludedColumns) > 0 THEN substring(@IncludedColumns, 1, len(@IncludedColumns)-1)
ELSE ''
END;
SET @TSQLScripCreationIndex = '';
SET @TSQLScripDisableIndex = '';
SET @DataCompressionType = SUBSTRING(@IndexOptions,CHARINDEX('DATA_COMPRESSION',@IndexOptions)+18,LEN(@IndexOptions));
SET @TSQLScripCreationIndex =
CASE WHEN @IndexTypeDesc LIKE '%COLUMNSTORE%' THEN
CASE
WHEN @IndexTypeDesc LIKE 'CLUSTERED%' THEN
'CREATE ' + @IndexTypeDesc + ' INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) +
'WITH (COMPRESSION_DELAY = ' + CONVERT(varchar,@compression_delay) + ', DATA_COMPRESSION = ' +
--SUBSTRING(@DataCompressionType,0,CHARINDEX(',',@DataCompressionType)) + ')
@DataCompressionType + ') ON ' + @FileGroupName + ';'
WHEN @is_filtered = 1 THEN
'CREATE ' + @IndexTypeDesc + ' INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' + + CHAR(13) +
'WHERE ' + @filter_definition + CHAR(13) + 'WITH (COMPRESSION_DELAY = ' + CONVERT(varchar,@compression_delay) + ', DATA_COMPRESSION = ' +
--SUBSTRING(@DataCompressionType,0,CHARINDEX(',',@DataCompressionType)) + ')
@DataCompressionType + ') ON ' + @FileGroupName + ';'
ELSE
'CREATE ' + @IndexTypeDesc + ' INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' +
CHAR(13) + 'WITH (COMPRESSION_DELAY = ' + CONVERT(varchar,@compression_delay) + ', DATA_COMPRESSION = ' +
--SUBSTRING(@DataCompressionType,0,CHARINDEX(',',@DataCompressionType)) + ')
@DataCompressionType + ') ON ' + @FileGroupName + ';' END
WHEN @IndexTypeDesc = 'MEMORY_OPTIMIZED' THEN
CASE WHEN @is_primary_key = 1 THEN
'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD CONSTRAINT ' + QUOTENAME(@IndexName) + ' ' + ' PRIMARY KEY NONCLUSTERED'
+ CHAR(13) + '(' + CHAR(13) + '' + @IndexColumns + CHAR(13) + ');'
WHEN @is_primary_key = 0 THEN
'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD INDEX ' + QUOTENAME(@IndexName) + ' ' + @is_unique + ' NONCLUSTERED'
+ CHAR(13) + '(' + CHAR(13) + '' + @IndexColumns + CHAR(13) + ');'END
WHEN @IndexTypeDesc = 'NONCLUSTERED HASH' THEN
CASE WHEN @is_primary_key = 1 THEN
(SELECT 'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD CONSTRAINT ' + QUOTENAME(@IndexName) + ' PRIMARY KEY NONCLUSTERED HASH'
+ CHAR(13) + '(' + CHAR(13) + '' + @IndexColumns + CHAR(13) + ') ' + 'WITH ( BUCKET_COUNT = ' + CONVERT(varchar,bucket_count) + ');'
FROM sys.hash_indexes WHERE object_id = OBJECT_ID(@SchemaName + '.' + @TableName) AND name = @IndexName)
WHEN @is_primary_key = 0 THEN
(SELECT 'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD INDEX ' + QUOTENAME(@IndexName) + ' ' + @IndexTypeDesc
+ CHAR(13) + '(' + CHAR(13) + '' + @IndexColumns + CHAR(13) + ') ' + 'WITH ( BUCKET_COUNT = ' + CONVERT(varchar,bucket_count) + ');'
FROM sys.hash_indexes WHERE object_id = OBJECT_ID(@SchemaName + '.' + @TableName) AND name = @IndexName) END
WHEN @IndexTypeDesc = 'XML' THEN
CASE WHEN EXISTS (SELECT name FROM sys.xml_indexes WHERE xml_index_type_description = 'PRIMARY_XML'
AND name = @IndexName AND object_id = OBJECT_ID(@SchemaName + '.' + @TableName)) THEN
'CREATE PRIMARY XML INDEX ' + QUOTENAME(@IndexName) + ' ON ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' + CHAR(13) + 'WITH (' + @IndexOptions+ ');'
WHEN EXISTS (SELECT object_id, index_id, name FROM sys.xml_indexes WHERE xml_index_type_description <> 'PRIMARY_XML'
AND name = @IndexName AND object_id = OBJECT_ID(@SchemaName + '.' + @TableName)) THEN
(SELECT 'CREATE XML INDEX ' + QUOTENAME(@IndexName) + ' ON ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ')' + CHAR(13) + 'USING XML INDEX ' + QUOTENAME(P.name)
+ ' FOR ' + I.secondary_type_desc collate latin1_general_cs_as + ' WITH (' + @IndexOptions+ ');'
FROM sys.xml_indexes I
INNER JOIN (SELECT object_id, index_id, name FROM sys.xml_indexes WHERE xml_index_type_description = 'PRIMARY_XML') P
ON P.object_id = I.object_id AND P.index_id = I.using_xml_index_id AND I.name = @IndexName)
END
WHEN @IndexTypeDesc = 'spatial' THEN
(SELECT 'CREATE SPATIAL INDEX ' + QUOTENAME(@IndexName) + ' ON ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ')' + 'USING ' + tessellation_scheme + CHAR(13)
+ 'WITH (BOUNDING_BOX =(' + CONVERT(varchar,bounding_box_xmin) + ', ' + CONVERT(varchar,bounding_box_ymin) + ', '
+ CONVERT(varchar,bounding_box_xmax) + ', ' + CONVERT(varchar,bounding_box_ymax)
+ '), GRIDS =(LEVEL1 = ' + level_1_grid_desc + ',LEVEL_2 = ' + level_2_grid_desc + ',LEVEL3 = ' +
level_3_grid_desc + ',LEVEL4 = ' + level_4_grid_desc + '),
CELLS_PER_OBJECT = ' + CONVERT(varchar,cells_per_object) + ', ' + @IndexOptions + ') ON ' + @FileGroupName + ';'
FROM sys.spatial_index_tessellations
WHERE object_id = OBJECT_ID(@SchemaName + '.' + @TableName) AND index_id =
(SELECT index_id from sys.indexes WHERE object_id = OBJECT_ID(@SchemaName + '.' + @TableName) AND name = @IndexName))
WHEN @is_filtered = 1 THEN
'CREATE ' + @is_unique + @IndexTypeDesc + ' INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) ++ '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' +
CASE
WHEN len(@IncludedColumns) > 0 THEN CHAR(13) + 'INCLUDE (' + @IncludedColumns+ ')'
ELSE ''
END +
CHAR(13) + 'WHERE ' + @filter_definition + CHAR(13) + 'WITH (' + @IndexOptions+ ') ON ' + @FileGroupName + ';'
WHEN @is_primary_key = 1 THEN
'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD CONSTRAINT ' + QUOTENAME(@IndexName) + ' PRIMARY KEY ' + @IndexTypeDesc
+ CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' + CHAR(13) + 'WITH (' + @IndexOptions+ ') ON ' + @FileGroupName + ';'
WHEN @is_unique_constraint = 1 THEN
'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD CONSTRAINT ' + QUOTENAME(@IndexName) + ' ' + @is_unique + @IndexTypeDesc
+ CHAR(13) + '(' + CHAR(13) + '' + @IndexColumns + CHAR(13) + ') ' + CHAR(13) + 'WITH (' + @IndexOptions+ ');'
ELSE
'CREATE '+ @is_unique + @IndexTypeDesc + ' INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' +
CASE
WHEN len(@IncludedColumns) > 0 THEN CHAR(13) + 'INCLUDE (' + @IncludedColumns+ ')'
ELSE ''
END +
CHAR(13) + 'WITH (' + @IndexOptions+ ') ON ' + @FileGroupName + ';'
END;
IF @is_disabled = 1
SET @TSQLScripDisableIndex = CHAR(13) +'ALTER INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' DISABLE;' + CHAR(13) + 'GO' + + CHAR(13)
PRINT @TSQLScripCreationIndex;
PRINT 'GO';
PRINT @TSQLScripDisableIndex;
FETCH NEXT
FROM CursorIndex
INTO @SchemaName,
@TableName,
@IndexName,
@is_unique,
@IndexTypeDesc,
@IndexOptions,
@is_disabled,
@FileGroupName,
@is_filtered,
@filter_definition,
@is_primary_key,
@is_unique_constraint,
@compression_delay
END;
CLOSE CursorIndex;
DEALLOCATE CursorIndex;
SQL Server 2012 and earlier:
/*
This script will generate the SQL to create all indexes in the database.
Copied from https://www.mssqltips.com/sqlservertip/3441/script-out-all-sql-server-indexes-in-a-database-using-tsql/.
Modified by GaryS 4-14-2016.
Modified to add ALTER TABLE for PK indexes and unique constraints, data compression, filtered indexes, XML indexes,
partitioned indexes, spatial indexes, and online options(when applicable).
NOTE: Does not script out Extended Properties (comments), or full-text indexes (and catalogs).
Also, due to several columns missing on older versions of SQL Server, I have commented out lines that only apply to
SQL Server 2014 or greater.
*/
DECLARE @SchemaName varchar(100);
DECLARE @TableName varchar(256);
DECLARE @IndexName varchar(256);
DECLARE @ColumnName varchar(100);
DECLARE @is_unique varchar(100);
DECLARE @IndexTypeDesc varchar(100);
DECLARE @FileGroupName varchar(100);
DECLARE @is_disabled varchar(100);
DECLARE @IndexOptions varchar(MAX);
DECLARE @DataCompressionType varchar(100);
DECLARE @is_filtered bit;
DECLARE @filter_definition varchar(MAX);
DECLARE @is_primary_key bit;
DECLARE @is_unique_constraint bit;
DECLARE @IndexColumnId int;
DECLARE @IsDescendingKey int;
DECLARE @IsIncludedColumn int;
DECLARE @compression_delay int;
DECLARE @TSQLScripCreationIndex varchar(MAX);
DECLARE @TSQLScripDisableIndex varchar(MAX);
DECLARE CursorIndex
CURSOR
FOR
--CTE to collect partitioned index information
WITH PartitionedIndexes AS (
SELECT t.object_id Object_ID,
t.name TableName,
ic.column_id PartitioningColumnID,
c.name PartitioningColumnName,
s.name AS [partition_scheme],
ix.name AS IndexName,
ix.index_id
FROM sys.tables t
INNER JOIN sys.indexes i ON i.object_id = t.object_id
INNER JOIN sys.index_columns ic ON ic.index_id = i.index_id
AND ic.object_id = t.object_id
INNER JOIN sys.columns c ON c.object_id = ic.object_id
AND c.column_id = ic.column_id
INNER JOIN sys.partition_schemes s ON i.data_space_id = s.data_space_id
INNER JOIN sys.indexes ix ON t.object_id=ix.object_id
AND ix.index_id = i.index_id
WHERE ic.partition_ordinal = 1
)
SELECT schema_name(t.schema_id) [schema_name],
t.name TableName,
ix.name IndexName,
CASE
WHEN ix.is_unique = 1 THEN 'UNIQUE '
ELSE ''
END IsUnique,
/*
--Commented out for older versions of SQL Server
CASE
WHEN t.object_id IN (SELECT OBJECT_ID FROM sys.tables WHERE is_memory_optimized = 1)
AND ix.type_desc <> 'NONCLUSTERED HASH'
THEN 'MEMORY_OPTIMIZED'
ELSE*/ ix.type_desc
--END
AS type_desc,
CASE
WHEN ix.is_padded=1 THEN 'PAD_INDEX = ON, '
ELSE 'PAD_INDEX = OFF, '
END +
CASE
WHEN INDEXPROPERTY(t.object_id, ix.name, 'IsStatistics') = 1 THEN 'STATISTICS_NORECOMPUTE = ON, '
ELSE 'STATISTICS_NORECOMPUTE = OFF, '
END +
CASE
WHEN (ix.is_primary_key = 1 OR ix.is_unique_constraint = 1) THEN ''
ELSE 'SORT_IN_TEMPDB = OFF, '
END +
CASE
WHEN ix.ignore_dup_key=1 THEN 'IGNORE_DUP_KEY = ON, '
ELSE 'IGNORE_DUP_KEY = OFF, '
END +
CASE
WHEN (@@Version LIKE '%ENTERPRISE%' AND (LOBTable.CanBeBuiltOnline IS NULL AND ix.type_desc = 'CLUSTERED')
AND ix.is_primary_key = 0 AND ix.is_unique_constraint = 0)
OR (@@Version LIKE '%ENTERPRISE%' AND (LOBIndexes.CanBeBuiltOnline IS NULL AND ix.type_desc <> 'CLUSTERED')
AND ix.is_primary_key = 0 AND ix.is_unique_constraint = 0)
THEN 'ONLINE = ON, '
ELSE ''
END +
CASE
WHEN ix.allow_row_locks=1 THEN 'ALLOW_ROW_LOCKS = ON, '
ELSE 'ALLOW_ROW_LOCKS = OFF, '
END +
CASE
WHEN ix.allow_page_locks=1 THEN 'ALLOW_PAGE_LOCKS = ON, '
ELSE 'ALLOW_PAGE_LOCKS = OFF, '
END +
CASE
WHEN CAST(ix.fill_factor AS VARCHAR(3)) = 0 THEN ''
ELSE 'FILLFACTOR =' + CAST(ix.fill_factor AS VARCHAR(3)) + ', '
END +
CASE
WHEN p.data_compression_desc IS NULL THEN 'DATA_COMPRESSION = NONE'
ELSE 'DATA_COMPRESSION = ' + p.data_compression_desc
END
AS IndexOptions,
ix.is_disabled,
CASE
WHEN ic.IsColumnPartitioned = 1 THEN '[' + PIdx.[partition_scheme] + ']' + '(' + '[' + PIdx.PartitioningColumnName + ']' + ')'
WHEN ic.IsColumnPartitioned = 0 THEN '[' + FILEGROUP_NAME(ix.data_space_id) + ']'
END AS FileGroupName,
ix.has_filter,
ix.filter_definition,
ix.is_primary_key,
ix.is_unique_constraint
/*
--Commented out for older versions of SQL Server
,ix.compression_delay*/
FROM sys.tables t
INNER JOIN sys.indexes ix ON t.object_id = ix.object_id
INNER JOIN
(SELECT DISTINCT OBJECT_ID,
index_id,
MAX(partition_ordinal) AS IsColumnPartitioned
FROM sys.index_columns
GROUP BY OBJECT_ID,
index_id) ic ON ic.index_id = ix.index_id
AND ic.object_id = t.object_id
LEFT JOIN
(SELECT DISTINCT object_id,
index_id,
data_compression_desc
FROM sys.partitions) p ON ix.object_id = p.object_id
AND ix.index_id = p.index_id
LEFT JOIN PartitionedIndexes PIdx ON PIdx.Object_ID = t.object_id
AND PIdx.index_id = ix.index_id
LEFT JOIN
(SELECT DISTINCT object_id,
0 AS CanBeBuiltOnline
FROM sys.columns c
INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE t.Name IN ('image',
'ntext',
'text',
'XML')
OR (t.Name IN ('varchar',
'nvarchar',
'varbinary')
AND c.max_length = -1)
OR c.is_filestream = 1) LOBTable ON LOBTable.object_id = t.object_id
LEFT JOIN
(SELECT DISTINCT c.object_id,
i.index_id,
0 AS CanBeBuiltOnline
FROM sys.columns c
INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN sys.index_columns ic ON ic.object_id = c.object_id
AND ic.column_id = c.column_id
INNER JOIN sys.indexes i ON ic.object_id = i.object_id
AND ic.index_id = i.index_id
WHERE t.Name IN ('image',
'ntext',
'text',
'XML')
OR (t.Name IN ('varchar',
'nvarchar',
'varbinary')
AND c.max_length = -1)
OR c.is_filestream = 1) LOBIndexes ON LOBIndexes.object_id = t.object_id
AND LOBIndexes.index_id = ix.index_id
WHERE
/*****************Ignores PK indexes************************/
--ix.type > 0 AND ix.is_primary_key = 0 AND ix.is_unique_constraint = 0 AND
/***********************************************************/
t.is_ms_shipped = 0
AND t.name <> 'sysdiagrams'
AND ix.name IS NOT NULL
/*****************Ignores clustered indexes*****************/
--AND ix.type_desc <> 'CLUSTERED'
/***********************************************************/
/*****************Ignores COLUMNSTORE indexes***************/
--AND ix.type_desc NOT LIKE '%COLUMNSTORE%'
/***********************************************************/
ORDER BY schema_name(t.schema_id),
t.name,
ix.name
OPEN CursorIndex
FETCH NEXT
FROM CursorIndex
INTO @SchemaName,
@TableName,
@IndexName,
@is_unique,
@IndexTypeDesc,
@IndexOptions,
@is_disabled,
@FileGroupName,
@is_filtered,
@filter_definition,
@is_primary_key,
@is_unique_constraint
/*
--Commented out for older versions of SQL Server
,@compression_delay*/
WHILE (@@fetch_status = 0)
BEGIN
DECLARE @IndexColumns varchar(MAX);
DECLARE @IncludedColumns varchar(MAX);
SET @IndexColumns='';
SET @IncludedColumns='';
SET @DataCompressionType = '';
DECLARE CursorIndexColumn
CURSOR
FOR
SELECT col.name,
ixc.is_descending_key,
ixc.is_included_column
FROM sys.tables tb
INNER JOIN sys.indexes ix ON tb.object_id = ix.object_id
INNER JOIN sys.index_columns ixc ON ix.object_id = ixc.object_id
AND ix.index_id = ixc.index_id
INNER JOIN sys.columns col ON ixc.object_id = col.object_id
AND ixc.column_id = col.column_id
WHERE
/*****************Ignores PK indexes************************/
--ix.type > 0 and (ix.is_primary_key = 0 or ix.is_unique_constraint = 0) AND
/***********************************************************/
schema_name(tb.schema_id) = @SchemaName
AND tb.name = @TableName
AND ix.name = @IndexName
/*****************Ignores clustered indexes*****************/
--AND ix.type_desc <> 'CLUSTERED'
/***********************************************************/
/*****************Ignores COLUMNSTORE indexes***************/
--AND ix.type_desc NOT LIKE '%COLUMNSTORE%'
/***********************************************************/
ORDER BY ixc.key_ordinal
OPEN CursorIndexColumn
FETCH NEXT
FROM CursorIndexColumn
INTO @ColumnName,
@IsDescendingKey,
@IsIncludedColumn
WHILE (@@fetch_status=0)
BEGIN
IF @IsIncludedColumn = 0
OR @IndexTypeDesc LIKE '%COLUMNSTORE%' OR @IndexTypeDesc = 'XML' OR @IndexTypeDesc = 'spatial'
OR @IndexTypeDesc = 'NONCLUSTERED HASH'
SET @IndexColumns =
CASE WHEN @IndexTypeDesc LIKE '%COLUMNSTORE%'
THEN @IndexColumns + '[' + @ColumnName + '], '
WHEN @IndexTypeDesc = 'XML' OR @IndexTypeDesc = 'spatial' OR @IndexTypeDesc = 'NONCLUSTERED HASH'
THEN @IndexColumns + '[' + @ColumnName + '], '
ELSE @IndexColumns + '[' + @ColumnName + ']' +
CASE
WHEN @IsDescendingKey = 1 THEN ' DESC, '
ELSE ' ASC, '
END
END
ELSE
SET @IncludedColumns = @IncludedColumns + '[' + @ColumnName + '], '
FETCH NEXT
FROM CursorIndexColumn
INTO @ColumnName,
@IsDescendingKey,
@IsIncludedColumn
END
CLOSE CursorIndexColumn
DEALLOCATE CursorIndexColumn
SET @IndexColumns = substring(@IndexColumns, 1, len(@IndexColumns)-1);
SET @IncludedColumns =
CASE
WHEN len(@IncludedColumns) > 0 THEN substring(@IncludedColumns, 1, len(@IncludedColumns)-1)
ELSE ''
END;
SET @TSQLScripCreationIndex = '';
SET @TSQLScripDisableIndex = '';
SET @DataCompressionType = SUBSTRING(@IndexOptions,CHARINDEX('DATA_COMPRESSION',@IndexOptions)+18,LEN(@IndexOptions));
SET @TSQLScripCreationIndex =
CASE WHEN @IndexTypeDesc LIKE '%COLUMNSTORE%' THEN
CASE
WHEN @IndexTypeDesc LIKE 'CLUSTERED%' THEN
'CREATE ' + @IndexTypeDesc + ' INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) +
'WITH (COMPRESSION_DELAY = ' + CONVERT(varchar,@compression_delay) + ', DATA_COMPRESSION = ' +
--SUBSTRING(@DataCompressionType,0,CHARINDEX(',',@DataCompressionType)) + ')
@DataCompressionType + ') ON ' + @FileGroupName + ';'
WHEN @is_filtered = 1 THEN
'CREATE ' + @IndexTypeDesc + ' INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' + + CHAR(13) +
'WHERE ' + @filter_definition + CHAR(13) + 'WITH (COMPRESSION_DELAY = ' + CONVERT(varchar,@compression_delay) + ', DATA_COMPRESSION = ' +
--SUBSTRING(@DataCompressionType,0,CHARINDEX(',',@DataCompressionType)) + ')
@DataCompressionType + ') ON ' + @FileGroupName + ';'
ELSE
'CREATE ' + @IndexTypeDesc + ' INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' +
CHAR(13) + 'WITH (COMPRESSION_DELAY = ' + CONVERT(varchar,@compression_delay) + ', DATA_COMPRESSION = ' +
--SUBSTRING(@DataCompressionType,0,CHARINDEX(',',@DataCompressionType)) + ')
@DataCompressionType + ') ON ' + @FileGroupName + ';' END
/*
--Commented out for older versions of SQL Server
WHEN @IndexTypeDesc = 'MEMORY_OPTIMIZED' THEN
CASE WHEN @is_primary_key = 1 THEN
'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD CONSTRAINT ' + QUOTENAME(@IndexName) + ' ' + ' PRIMARY KEY NONCLUSTERED'
+ CHAR(13) + '(' + CHAR(13) + '' + @IndexColumns + CHAR(13) + ');'
WHEN @is_primary_key = 0 THEN
'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD INDEX ' + QUOTENAME(@IndexName) + ' ' + @is_unique + ' NONCLUSTERED'
+ CHAR(13) + '(' + CHAR(13) + '' + @IndexColumns + CHAR(13) + ');'END
WHEN @IndexTypeDesc = 'NONCLUSTERED HASH' THEN
CASE WHEN @is_primary_key = 1 THEN
(SELECT 'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD CONSTRAINT ' + QUOTENAME(@IndexName) + ' PRIMARY KEY NONCLUSTERED HASH'
+ CHAR(13) + '(' + CHAR(13) + '' + @IndexColumns + CHAR(13) + ') ' + 'WITH ( BUCKET_COUNT = ' + CONVERT(varchar,bucket_count) + ');'
FROM sys.hash_indexes WHERE object_id = OBJECT_ID(@SchemaName + '.' + @TableName) AND name = @IndexName)
WHEN @is_primary_key = 0 THEN
(SELECT 'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD INDEX ' + QUOTENAME(@IndexName) + ' ' + @IndexTypeDesc
+ CHAR(13) + '(' + CHAR(13) + '' + @IndexColumns + CHAR(13) + ') ' + 'WITH ( BUCKET_COUNT = ' + CONVERT(varchar,bucket_count) + ');'
FROM sys.hash_indexes WHERE object_id = OBJECT_ID(@SchemaName + '.' + @TableName) AND name = @IndexName) END
*/
WHEN @IndexTypeDesc = 'XML' THEN
CASE WHEN EXISTS (SELECT name FROM sys.xml_indexes WHERE xml_index_type_description = 'PRIMARY_XML'
AND name = @IndexName AND object_id = OBJECT_ID(@SchemaName + '.' + @TableName)) THEN
'CREATE PRIMARY XML INDEX ' + QUOTENAME(@IndexName) + ' ON ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' + CHAR(13) + 'WITH (' + @IndexOptions+ ');'
WHEN EXISTS (SELECT object_id, index_id, name FROM sys.xml_indexes WHERE xml_index_type_description <> 'PRIMARY_XML'
AND name = @IndexName AND object_id = OBJECT_ID(@SchemaName + '.' + @TableName)) THEN
(SELECT 'CREATE XML INDEX ' + QUOTENAME(@IndexName) + ' ON ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ')' + CHAR(13) + 'USING XML INDEX ' + QUOTENAME(P.name)
+ ' FOR ' + I.secondary_type_desc collate latin1_general_cs_as + ' WITH (' + @IndexOptions+ ');'
FROM sys.xml_indexes I
INNER JOIN (SELECT object_id, index_id, name FROM sys.xml_indexes WHERE xml_index_type_description = 'PRIMARY_XML') P
ON P.object_id = I.object_id AND P.index_id = I.using_xml_index_id AND I.name = @IndexName)
END
WHEN @IndexTypeDesc = 'spatial' THEN
(SELECT 'CREATE SPATIAL INDEX ' + QUOTENAME(@IndexName) + ' ON ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ')' + 'USING ' + tessellation_scheme + CHAR(13)
+ 'WITH (BOUNDING_BOX =(' + CONVERT(varchar,bounding_box_xmin) + ', ' + CONVERT(varchar,bounding_box_ymin) + ', '
+ CONVERT(varchar,bounding_box_xmax) + ', ' + CONVERT(varchar,bounding_box_ymax)
+ '), GRIDS =(LEVEL1 = ' + level_1_grid_desc + ',LEVEL_2 = ' + level_2_grid_desc + ',LEVEL3 = ' +
level_3_grid_desc + ',LEVEL4 = ' + level_4_grid_desc + '),
CELLS_PER_OBJECT = ' + CONVERT(varchar,cells_per_object) + ', ' + @IndexOptions + ') ON ' + @FileGroupName + ';'
FROM sys.spatial_index_tessellations
WHERE object_id = OBJECT_ID(@SchemaName + '.' + @TableName) AND index_id =
(SELECT index_id from sys.indexes WHERE object_id = OBJECT_ID(@SchemaName + '.' + @TableName) AND name = @IndexName))
WHEN @is_filtered = 1 THEN
'CREATE ' + @is_unique + @IndexTypeDesc + ' INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) ++ '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' +
CASE
WHEN len(@IncludedColumns) > 0 THEN CHAR(13) + 'INCLUDE (' + @IncludedColumns+ ')'
ELSE ''
END +
CHAR(13) + 'WHERE ' + @filter_definition + CHAR(13) + 'WITH (' + @IndexOptions+ ') ON ' + @FileGroupName + ';'
WHEN @is_primary_key = 1 THEN
'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD CONSTRAINT ' + QUOTENAME(@IndexName) + ' PRIMARY KEY ' + @IndexTypeDesc
+ CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' + CHAR(13) + 'WITH (' + @IndexOptions+ ') ON ' + @FileGroupName + ';'
WHEN @is_unique_constraint = 1 THEN
'ALTER TABLE ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' ADD CONSTRAINT ' + QUOTENAME(@IndexName) + ' ' + @is_unique + @IndexTypeDesc
+ CHAR(13) + '(' + CHAR(13) + '' + @IndexColumns + CHAR(13) + ') ' + CHAR(13) + 'WITH (' + @IndexOptions+ ');'
ELSE
'CREATE '+ @is_unique + @IndexTypeDesc + ' INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + CHAR(13) + '(' + CHAR(13) +
'' + @IndexColumns + CHAR(13) + ') ' +
CASE
WHEN len(@IncludedColumns) > 0 THEN CHAR(13) + 'INCLUDE (' + @IncludedColumns+ ')'
ELSE ''
END +
CHAR(13) + 'WITH (' + @IndexOptions+ ') ON ' + @FileGroupName + ';'
END;
IF @is_disabled = 1
SET @TSQLScripDisableIndex = CHAR(13) +'ALTER INDEX ' + QUOTENAME(@IndexName) + ' ON ' +
QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName) + ' DISABLE;' + CHAR(13) + 'GO' + + CHAR(13)
PRINT @TSQLScripCreationIndex;
PRINT 'GO';
PRINT @TSQLScripDisableIndex;
FETCH NEXT
FROM CursorIndex
INTO @SchemaName,
@TableName,
@IndexName,
@is_unique,
@IndexTypeDesc,
@IndexOptions,
@is_disabled,
@FileGroupName,
@is_filtered,
@filter_definition,
@is_primary_key,
@is_unique_constraint
/*
--Commented out for older versions of SQL Server
,@compression_delay */
END;
CLOSE CursorIndex;
DEALLOCATE CursorIndex;
|