How to display indexes on a SQL Server table?

I need to list / display all clustered and small indexes contained in a table.

How can I do this using SQL Server 2008 R2?

+3


source to share


1 answer


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 (such as the columns contained in the index, their datatype, etc.), you can expand your query 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

      

The views in the system catalog contain a lot of information about your system ....

+1


source







All Articles