I need to list/display all the clustered and non clustered indexes contained by a table.
How can I do that using SQL Server 2008 R2?
How about this:
SELECT
TableName = t.Name,
i.*
FROM
sys.indexes i
INNER JOIN
sys.tables t ON t.object_id = i.object_id
WHERE
T.Name = 'YourTableName'
If you need more information (like columns contained in the index, their datatype etc.) - you can expand your query to something like this:
SELECT
TableName = t.Name,
IndexName = i.Name,
IndexType = i.type_desc,
ColumnOrdinal = Ic.key_ordinal,
ColumnName = c.name,
ColumnType = ty.name
FROM
sys.indexes i
INNER JOIN
sys.tables t ON t.object_id = i.object_id
INNER JOIN
sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
INNER JOIN
sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
INNER JOIN
sys.types ty ON c.system_type_id = ty.system_type_id
WHERE
t.name = 'YourTableName'
ORDER BY
t.Name, i.name, ic.key_ordinal
These system catalog views contain a wealth of information about your system....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With