I wrote the following that gets the create statements for table indexes for SQL Server database.
-- Primary Key
SELECT
-- DISTINCT
CONCAT(
'if exists ',
'( ',
'select * from sys.indexes where name =''',SI.NAME,''' ',
') ',
'BEGIN ',
'DROP INDEX ',ST.NAME,'.',SI.NAME,' ',
'END; ',
' ',
'if NOT exists ',
'( ',
'select * from sys.indexes where name =''',SI.NAME,''' ',
') ',
'BEGIN ',
'ALTER TABLE [',
SCH.NAME,
'].[',
ST.NAME,
'] ADD CONSTRAINT [',
SI.NAME,
'] PRIMARY KEY ',
-- CASE WHEN SI.is_unique = 1 THEN 'UNIQUE ' END,
SI.TYPE_DESC COLLATE SQL_Latin1_General_CP1_CI_AS,
CHAR(10),char(13),
' (',
CD.COLUMNS,
')',
CASE WHEN LEN(CD2.COLUMNS) <> 0
THEN
CONCAT(
'INCLUDE(',
CD2.COLUMNS,
')'
)
END,
' WITH (',
LEFT(SI2.PARAMS,LEN(SI2.PARAMS)-1),
') ON [PRIMARY]',
'END;'
) CREATE_STATEMENT,
1 LastColumn
FROM SYS.indexes SI
outer apply(
select
CONCAT(
CASE WHEN SI.is_padded = 1 THEN 'PAD_INDEX = ON, ' ELSE 'PAD_INDEX = OFF,' END,
'STATISTICS_NORECOMPUTE = OFF, ',
'SORT_IN_TEMPDB = OFF, ',
'ONLINE = OFF, ',
CASE WHEN SI.allow_row_locks = 1 THEN 'ALLOW_ROW_LOCKS = ON, ' ELSE 'ALLOW_ROW_LOCKS = OFF, 'END,
CASE WHEN SI.allow_page_locks = 1 THEN 'ALLOW_PAGE_LOCKS = ON, ' ELSE 'ALLOW_PAGE_LOCKS = OFF, ' END,
CASE WHEN SI.fill_factor <> 0 THEN CONCAT('FILLFACTOR = ', SI.fill_factor,', ') END,
CASE WHEN SI.OPTIMIZE_FOR_SEQUENTIAL_KEY <> 0 THEN 'OPTIMIZE_FOR_SEQUENTIAL_KEY = ON, ' ELSE 'OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF, ' END
) PARAMS
FROM
SYS.INDEXES SI2
WHERE
1=1
AND SI2.OBJECT_ID = SI.OBJECT_ID
AND SI2.INDEX_ID = SI.INDEX_ID
) SI2
INNER JOIN SYS.TABLES ST ON
1=1
and ST.object_id = SI.object_id
inner join sys.schemas sch on
1=1
and sch.schema_id = st.schema_id
-- Columns in index
outer apply (
select
STRING_AGG(CONCAT(
'[',
SC2.NAME,
'] ',
case when sic2.is_descending_key = 1 then 'DESC' else 'ASC' end
),',') WITHIN GROUP ( ORDER BY sic2.KEY_ORDINAL, sic2.COLUMN_ID) COLUMNS
from
sys.index_columns sic2
INNER JOIN SYS.COLUMNS SC2 ON
1=1
AND SC2.object_id = ST.object_id
AND SC2.column_id = SIC2.column_id
where
1=1
AND SIC2.object_id = SI.object_id
AND SIC2.index_id = SI.index_id
AND SIC2.is_included_column = 0
) CD
outer apply (
select
STRING_AGG(CONCAT(
'[',
SC2.NAME,
'] '
),',') WITHIN GROUP ( ORDER BY sic2.KEY_ORDINAL, sic2.COLUMN_ID) COLUMNS
from
sys.index_columns sic2
INNER JOIN SYS.COLUMNS SC2 ON
1=1
AND SC2.object_id = ST.object_id
AND SC2.column_id = SIC2.column_id
where
1=1
AND SIC2.object_id = SI.object_id
AND SIC2.index_id = SI.index_id
AND SIC2.is_included_column = 1
) CD2
WHERE
1=1
AND ST.NAME = '<<table_name>>'
AND SI.IS_PRIMARY_KEY = 1
-- NonPrimary Key
SELECT
-- DISTINCT
CONCAT(
'if exists ',
'( ',
'select * from sys.indexes where name =''',SI.NAME,''' ',
') ',
'BEGIN ',
'DROP INDEX ',ST.NAME,'.',SI.NAME,' ',
'END; ',
' ',
'if NOT exists ',
'( ',
'select * from sys.indexes where name =''',SI.NAME,''' ',
') ',
'BEGIN ',
'CREATE ',
CASE WHEN SI.is_unique = 1 THEN 'UNIQUE ' END,
SI.TYPE_DESC COLLATE SQL_Latin1_General_CP1_CI_AS,
' INDEX [',
SI.NAME,
'] ON [',
sch.name,
'].[',
st.name,
']',
CHAR(10),char(13),
'(',
CD.COLUMNS,
')',
CASE WHEN LEN(CD2.COLUMNS) <> 0
THEN
CONCAT(
'INCLUDE(',
CD2.COLUMNS,
')'
)
END,
' WITH (',
LEFT(SI2.PARAMS,LEN(SI2.PARAMS)-1),
') ',
'END;'
) CREATE_STATEMENT,
1 LastColumn
FROM SYS.indexes SI
outer apply(
select
CONCAT(
CASE WHEN SI.is_padded = 1 THEN 'PAD_INDEX = ON, ' ELSE 'PAD_INDEX = OFF,' END,
'STATISTICS_NORECOMPUTE = OFF, ',
'SORT_IN_TEMPDB = OFF, ',
'ONLINE = OFF, ',
CASE WHEN SI.allow_row_locks = 1 THEN 'ALLOW_ROW_LOCKS = ON, ' ELSE 'ALLOW_ROW_LOCKS = OFF, 'END,
CASE WHEN SI.allow_page_locks = 1 THEN 'ALLOW_PAGE_LOCKS = ON, ' ELSE 'ALLOW_PAGE_LOCKS = OFF, ' END,
CASE WHEN SI.fill_factor <> 0 THEN CONCAT('FILLFACTOR = ', SI.fill_factor,', ') END,
CASE WHEN SI.OPTIMIZE_FOR_SEQUENTIAL_KEY <> 0 THEN 'OPTIMIZE_FOR_SEQUENTIAL_KEY = ON, ' ELSE 'OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF, ' END
) PARAMS
FROM
SYS.INDEXES SI2
WHERE
1=1
AND SI2.OBJECT_ID = SI.OBJECT_ID
AND SI2.INDEX_ID = SI.INDEX_ID
) SI2
INNER JOIN SYS.TABLES ST ON
1=1
and ST.object_id = SI.object_id
inner join sys.schemas sch on
1=1
and sch.schema_id = st.schema_id
-- Columns in index
outer apply (
select
STRING_AGG(CONCAT(
'[',
SC2.NAME,
'] ',
case when sic2.is_descending_key = 1 then 'DESC' else 'ASC' end
),',') WITHIN GROUP ( ORDER BY sic2.KEY_ORDINAL, sic2.COLUMN_ID) COLUMNS
from
sys.index_columns sic2
INNER JOIN SYS.COLUMNS SC2 ON
1=1
AND SC2.object_id = ST.object_id
AND SC2.column_id = SIC2.column_id
where
1=1
AND SIC2.object_id = SI.object_id
AND SIC2.index_id = SI.index_id
AND SIC2.is_included_column = 0
) CD
outer apply (
select
STRING_AGG(CONCAT(
'[',
SC2.NAME,
'] '
),',') WITHIN GROUP ( ORDER BY sic2.KEY_ORDINAL, sic2.COLUMN_ID) COLUMNS
from
sys.index_columns sic2
INNER JOIN SYS.COLUMNS SC2 ON
1=1
AND SC2.object_id = ST.object_id
AND SC2.column_id = SIC2.column_id
where
1=1
AND SIC2.object_id = SI.object_id
AND SIC2.index_id = SI.index_id
AND SIC2.is_included_column = 1
) CD2
WHERE
1=1
AND ST.NAME = '<<table_name>>'
AND SI.IS_PRIMARY_KEY = 0