Databasedeki Tüm İndexleri Create Kodu ile Alma SQL Script
Bu script ile Sql Server daki Database ait tüm NONCLUSTERED indexleri listeleyip create kodlarını alabilirsiniz
SELECT
DB_NAME() AS DatabaseName, -- Veritabanı adı
OBJECT_SCHEMA_NAME(i.object_id) + '.' + OBJECT_NAME(i.object_id) AS TableName, -- Şema ve tablo adı
i.name AS IndexName, -- İndeks adı
'CREATE ' +
CASE WHEN i.is_unique = 1 THEN 'UNIQUE ' ELSE '' END +
'NONCLUSTERED INDEX [' + i.name + '] ON ' +
OBJECT_SCHEMA_NAME(i.object_id) + '.' + OBJECT_NAME(i.object_id) +
' (' +
STUFF((
SELECT ', ' + c.name
FROM sys.index_columns ic
JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE ic.object_id = i.object_id AND ic.index_id = i.index_id AND ic.is_included_column = 0
ORDER BY ic.key_ordinal
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '') +
')' +
CASE
WHEN EXISTS (
SELECT 1
FROM sys.index_columns ic2
WHERE ic2.object_id = i.object_id AND ic2.index_id = i.index_id AND ic2.is_included_column = 1
) THEN
' INCLUDE (' +
STUFF((
SELECT ', ' + c2.name
FROM sys.index_columns ic2
JOIN sys.columns c2 ON ic2.object_id = c2.object_id AND ic2.column_id = c2.column_id
WHERE ic2.object_id = i.object_id AND ic2.index_id = i.index_id AND ic2.is_included_column = 1
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '') +
')'
ELSE ''
END AS CreateIndexScript
FROM
sys.indexes i
WHERE
i.type = 2 -- Non-clustered indeksler
ORDER BY
DatabaseName, TableName, IndexName;