我曾尝试 sp_helpindex ,但它显示的索引列不是包含的列。请告诉我如何列出包含列(非键)的所有索引?
答案 0 :(得分:26)
针对目录视图尝试此T-SQL查询:
SELECT
IndexName = i.Name,
ColName = c.Name
FROM
sys.indexes i
INNER JOIN
sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
INNER JOIN
sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
WHERE
ic.is_included_column = 1
ORDER BY
i.Name
它基本上检查所有索引(sys.indexes
),然后链接到它们的列并检查哪些列被设计为包含列(ic.is_included_column = 1
),然后列出所有这些索引和所有这些列
答案 1 :(得分:9)
从http://www.midnightdba.com/Jen/2009/12/get-index-included-column-info/
复制SELECT
OBJECT_NAME(i.[object_id]) TableName ,
i.[name] IndexName ,
c.[name] ColumnName ,
ic.is_included_column ,
i.index_id ,
i.type_desc ,
i.is_unique ,
i.data_space_id ,
i.ignore_dup_key ,
i.is_primary_key ,
i.is_unique_constraint
FROM
sys.indexes i
JOIN sys.index_columns ic ON ic.object_id = i.object_id and i.index_id = ic.index_id
JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
ORDER BY
tableName ,
ic.index_id ,
ic.index_column_id
答案 2 :(得分:0)
如果您希望全局系统存储过程可用于获取索引键/非键(包括)列,请运行以下代码
USE master
GO
CREATE PROCEDURE sp_helpcolindex
@objname NVARCHAR(776)
AS
BEGIN
SELECT
c.Name,
CASE ic.is_included_column WHEN 0 THEN 'Key' ELSE 'Include' END AS [Type]
FROM sys.indexes i
JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
WHERE i.name = @objname
ORDER BY [Type] DESC
END
GO
EXEC sp_ms_marksystemobject 'sp_helpcolindex'
GO