Skip to content

Instantly share code, notes, and snippets.

@NaserKhoshfetrat
Forked from timabell/list-indexes.sql
Created September 13, 2021 09:00
Show Gist options
  • Save NaserKhoshfetrat/61c251f1cb5e186248acc9a2519ef91f to your computer and use it in GitHub Desktop.
Save NaserKhoshfetrat/61c251f1cb5e186248acc9a2519ef91f to your computer and use it in GitHub Desktop.
-- list indexes
-- http://stackoverflow.com/a/428235/10245
-- https://gist.github.com/timabell/6da05a2b2542c907f179
select
o.name as TableName,
i.name as IndexName,
i.type,
i.is_unique,
ic.key_ordinal as ColumnOrder,
ic.is_included_column as IsIncluded,
co.[name] as ColumnName
from sys.indexes i
join sys.objects o on i.object_id = o.object_id
join sys.index_columns ic on ic.object_id = i.object_id
and ic.index_id = i.index_id
join sys.columns co on co.object_id = i.object_id
and co.column_id = ic.column_id
where
i.is_primary_key = 0
and o.[type] = 'U'
--and ic.is_included_column = 0
order by o.[name], i.[name], ic.is_included_column, ic.key_ordinal;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment