July 30, 2008

Script all indexes ( except PK ) of a table

Script all indexes ( except PK ) of a table :
SELECT OBJECT_NAME(SI.Object_ID) AS TABLE_NAME, SI.Name
FROM Sys.Indexes SI
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
ON SI.Name = TC.CONSTRAINT_NAME AND OBJECT_NAME(SI.Object_ID) = TC.TABLE_NAME
WHERE TC.CONSTRAINT_NAME IS NULL
AND OBJECTPROPERTY(SI.Object_ID, 'IsUserTable') = 1
AND OBJECT_NAME(SI.Object_ID)
in ('Table_Names')
ORDER BY OBJECT_NAME(SI.Object_ID), SI.Index_ID

No comments: