Thursday, December 29, 2011

How to get indexes on a database

The following query returns the indexes used on tables in a database. I think it's very useful query for all the sequel server guys.We can use the query result of  'IndexRebuild' column for rebuild index.

SELECT
    OBJECT_SCHEMA_NAME(OBJECT_ID) As [Schema],
    OBJECT_NAME(OBJECT_ID) [Table],
    Name,
    'ALTER INDEX ' + name +  ' ON '+object_schema_name(OBJECT_ID)+'.'+OBJECT_NAME(OBJECT_ID)+' REBUILD;' As IndexRebuild
    ,*
FROM    sys.indexes
WHERE   TYPE > 0
ORDER BY
OBJECT_SCHEMA_NAME(OBJECT_ID),
OBJECT_NAME(OBJECT_ID)

The following system stored procedure returns the index information of a single table.

EXEC sp_helpindex 'TableName'

No comments:

Post a Comment