检查索引是否存在

时间:2021-04-29 14:39:11

标签: database-indexes sql-server-2019

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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你的问题有点含糊,但如果我理解正确,你想在索引有两列或更多列的所有表上找到索引:

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