表包含全部索引的索引结构

表包含全部索引的索引结构DECLARE @tbl nvarchar(265) SELECT @tbl = '' SELECT o.name,i.index_id, i.name, i.type_desc, s

表包含全部索引的索引结构

DECLARE @tbl nvarchar(265)
SELECT @tbl = ""
 
SELECT o.name,i.index_id, i.name, i.type_desc,
       substring(ikey.cols, 3, len(ikey.cols))AS key_cols,
       substring(inc.cols, 3, len(inc.cols)) ASincluded_cols,
       stats_date(o.object_id, i.index_id) ASstats_date,
       i.filter_definition
FROM   sys.objects o
JOIN   sys.indexes i ON i.object_id = o.object_id
CROSS  APPLY (SELECT ", " + c.name +
                     CASE ic.is_descending_key
                          WHEN 1 THEN " DESC"
                          ELSE ""
                     END
              FROM   sys.index_columns ic
              JOIN   sys.columns c ON ic.object_id = c.object_id
                                  AND ic.column_id = c.column_id
              WHERE  ic.object_id = i.object_id
                AND  ic.index_id = i.index_id
                AND  ic.is_included_column = 0
              ORDER BY ic.key_ordinal
              FOR XML PATH("")) AS ikey(cols)
OUTER  APPLY (SELECT ", " + c.name
              FROM   sys.index_columns ic
              JOIN   sys.columns c ON ic.object_id = c.object_id
                                  AND ic.column_id = c.column_id
              WHERE  ic.object_id = i.object_id
                AND  ic.index_id = i.index_id
                AND  ic.is_included_column = 1
              ORDER  BY ic.index_column_id
              FOR XML PATH("")) AS inc(cols)
WHERE  o.name = @tbl
  AND i.type IN (1, 2)
ORDER  BY o.name, i.index_id

代码100分

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/7049.html

(0)
上一篇 2023-03-28 12:30
下一篇 2023-03-28 13:30

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注