首页 > 解决方案 > 检查索引是否存在

问题描述

如果索引有 2 列或更多列,如何检查 SQL Server 上是否已有索引?我不想只检查名称我想按表按列检查

任何帮助,将不胜感激。

标签: database-indexessql-server-2019

解决方案


Your question is a little vague, but if I understand correctly, you want to find indexes on all tables where the index has two or more columns:

select t.[name] TableName, i.[name] IndexName
from (
    select object_id, Index_Id
    from sys.index_columns
        group by object_Id, Index_Id
    having Count(*)>1
)x 
join sys.indexes i on i.index_id=x.index_id and i.object_id=x.object_id
join sys.tables t on t.object_id=x.object_id and t.[type]='U'
order by Tablename, IndexName

推荐阅读