Welcome to SqlAdvice Sign in | Join | Help

Retrieve Index Details and Columns in Sql Server 2005

I frequently need to look at what indexes are on a table and I am really not a fan of using object explorer in SSMS as I like to stay in the query window.  Typically if I want information about a table I simply highlight the table name in the query window and hit Alt-F1.  If you aren't familiar with keyboard shortcuts in SSMS see my post here.  While this does give you the indexes that are on the table and it will show you the equality and inequality columns on the index, it won't show you the included columns on the index.  So I decided to dig into the Sql Server Dynamic Management Views (DMVs) and write a query to return all the information about an index or even all indexes on a table.  Here is what I came up with.  This will return all indexes for a table. 

 

select object_name(i.object_id) as TableName, i.name as IndexName, c.name as ColumnName, i.Type_Desc, ic.Is_Included_Column, key_ordinal from sys.indexes i join sys.Index_columns ic on ic.object_id = i.object_id and ic.index_id = i.index_id join sys.columns c on c.object_id = ic.object_id and c.column_id = ic.column_id where object_name(i.object_id) = 'MyTable' order by i.type_desc, i.name, Is_Included_Column asc, ic.key_ordinal

Let me know if you have any questions or improvements. 

Sponsor
Published Wednesday, October 10, 2007 9:49 AM by gstark

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Monday, November 26, 2007 6:36 PM by Retrieve Index Details and Columns in Sql Server 2005

# Retrieve Index Details and Columns in Sql Server 2005

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below