Files
DBAdmin/inbox/DataBase/SQLServer/index/idx_all.sql
T
2026-05-18 06:40:19 +00:00

22 lines
1.1 KiB
SQL

---------------------------------------------------------------------------------------------------------------------------
-- Purpose: This query will list all indexes in the database and show index columns and included columns using STRING_AGG (SQL 2017 and later). Run this in the user database.
-- More Information:
-- Revision: 2019-05-21
--
SELECT
SCHEMA_NAME(ss.SCHEMA_id) AS SCHEMANAME,
ss.name as TableName,
ss2.name as IndexName,
ss2.index_id,
(SELECT STRING_AGG(name,', ')
FROM sys.index_columns a INNER JOIN sys.all_columns b ON a.object_id = b.object_id and a.column_id = b.column_id and a.object_id = ss.object_id and a.index_id = ss2.index_id and is_included_column = 0
) as IndexColumns,
(SELECT STRING_AGG(name,', ')
FROM sys.index_columns a INNER JOIN sys.all_columns b ON a.object_id = b.object_id and a.column_id = b.column_id and a.object_id = ss.object_id and a.index_id = ss2.index_id and is_included_column = 1
) as IncludedColumns
FROM sys.objects SS INNER JOIN sys.indexes ss2 ON ss.OBJECT_ID = ss2.OBJECT_ID
WHERE ss.type = 'U'
ORDER BY 1, 2, 3