我想知道在数据库中列出所有表的所有索引的最简单方法是。
我应该为每个表调用sp_helpindex并将结果存储在临时表中,还是有更简单的方法?
任何人都可以解释为什么约束存储在sysobjects中,但索引不是?
解决方法
以下是您需要的查询类型的示例:
select i.name as IndexName,o.name as TableName,ic.key_ordinal as ColumnOrder,ic.is_included_column as IsIncluded,co.[name] as ColumnName from sys.indexes i join sys.objects o on i.object_id = o.object_id join sys.index_columns ic on ic.object_id = i.object_id and ic.index_id = i.index_id join sys.columns co on co.object_id = i.object_id and co.column_id = ic.column_id where i.[type] = 2 and i.is_unique = 0 and i.is_primary_key = 0 and o.[type] = 'U' --and ic.is_included_column = 0 order by o.[name],i.[name],ic.is_included_column,ic.key_ordinal ;
这一点对某个目的有些具体(我在一个C#应用程序中使用它来找到重复的索引并格式化输出,因此它实际上是可读的)。但是你可以很容易地适应你的需要。