SQL SERVER 根据表名查找唯一索引、主键、索引的所有字段

SELECT a.name IndexName,d.COLUMN_NAME,d.ORDINAL_POSITION
FROM sys.indexes a (NOLOCK)
INNER JOIN sys.index_columns b (NOLOCK) ON a.object_id = b.object_id and a.index_id = b.index_id 
INNER JOIN sysindexkeys c (NOLOCK) ON a.object_id = c.id and b.index_id = c.indid and b.column_id = c.colid
INNER JOIN INFORMATION_SCHEMA.COLUMNS d (NOLOCK) ON a.object_id = object_id(d.TABLE_NAME) and c.keyno = d.ORDINAL_POSITION
WHERE a.object_id = object_id(N'替换成需要查找的表')
/*and a.is_primary_key = 1  主键索引*/
/*and a.is_unique = 1 唯一索引*/
ORDER BY a.name,d.ORDINAL_POSITION

 

你可能感兴趣的:(SQL,SERVE)