远离<代码>INFORMATION_SCHEMA.COLUMNS,特别是在索引方面,因为诸如过滤指数和列入一栏等内容不属于定义的范围。 我在此更详细地谈到这一点:
页: 1 例如:
DECLARE @tablename NVARCHAR(512) = dbo.tbl_ClientDN ;
SELECT
[Index] = i.name,
[Column] = c.Name,
[Type] = i.type_desc,
PK = i.is_primary_key,
[Unique] = i.is_unique,
[Unique Constraint] = i.is_unique_constraint,
[DESC] = ic.is_descending_key,
[INCLUDE] = ic.is_included_column,
[Filtered] = i.filter_definition -- only for SQL Server 2008+
FROM
sys.indexes AS i
INNER JOIN
sys.index_columns AS ic
ON i.[object_id] = ic.[object_id]
AND i.index_id = ic.index_id
INNER JOIN
sys.columns c
ON ic.column_id = c.column_id
AND ic.[object_id] = c.[object_id]
WHERE
i.[object_id] = OBJECT_ID(@tablename)
ORDER BY [Index], ic.index_column_id;
如果你想一劳永逸地对所有表格都这样做,那么简单改动:
SELECT
[Table] = QUOTENAME(OBJECT_SCHEMA_NAME(i.[object_id]))
+ . + QUOTENAME(OBJECT_NAME(i.[object_id])),
[Index] = i.name,
[Column] = c.Name,
[Type] = i.type_desc,
PK = i.is_primary_key,
[Unique] = i.is_unique,
[Unique Constraint] = i.is_unique_constraint,
[DESC] = ic.is_descending_key,
[INCLUDE] = ic.is_included_column,
[Filtered] = i.filter_definition -- only for SQL Server 2008+
FROM
sys.indexes AS i
INNER JOIN
sys.index_columns AS ic
ON i.[object_id] = ic.[object_id]
AND i.index_id = ic.index_id
INNER JOIN
sys.columns c
ON ic.column_id = c.column_id
AND ic.[object_id] = c.[object_id]
ORDER BY [Table], [Index], ic.index_column_id;
https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-indexes-transact-sql”rel=“nofollow noretinger”>sys.indexes 和
您也不妨对 Kimberley L. Tripp s sp_helpindex2。
http://www.ohchr.org。
总的来说,我同意“Brian White s”的意见。 如果你全力以赴,你就应该利用这一工具,而不是重新发明轮椅,并试图书写轮椅。 或许你在时间上已经花掉了好工具的成本。 请改为: