10

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?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Sarfaraz Makandar
  • 5,933
  • 16
  • 59
  • 84

1 Answers1

18

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....

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459