开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 9510|回复: 0

查询表索引

[复制链接]

0

精华

597

贡献

110

赞扬

赞助者组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
18
软币
336
在线时间
19 小时
注册时间
2017-8-3
发表于 2020-10-15 17:34:13 | 显示全部楼层 |阅读模式
--修改表名为具体表名
CREATE TABLE #Table(name NVARCHAR(256))
INSERT INTO #Table
SELECT '表名1' UNION ALL
SELECT '表名2' UNION ALL
SELECT '表名3' UNION ALL
SELECT '表名4'

SELECT 'IF EXISTS(SELECT TOP 1 1 FROM sys.indexes WHERE name='''+t.name+''') DROP INDEX IX_esBase_Inspect_InfoPoint ON dbo.'+tob.name+'; CREATE NONCLUSTERED INDEX '+t.name+' ON dbo.'+tob.name+' ( '+tc.name+' ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY];',   tob.name AS fldTableName, t.name AS fldIndex, tc.name AS fldIndexColumn,t.data_space_id
        FROM sys.indexes t
        INNER JOIN sys.objects  tob ON tob.object_id = t.object_id
        INNER JOIN #Table tab ON tab.name = tob.name
        OUTER APPLY(SELECT ISNULL((SELECT STUFF((SELECT ','+ name FROM (SELECT CAST(name AS NVARCHAR(256)) AS name FROM sys.columns a  INNER JOIN sys.index_columns tci ON a.column_id = tci.column_id AND a.object_id = tci.object_id WHERE tci.index_id = t.index_id AND tci.object_id = t.object_id )ty FOR XML PATH('')),1,1,'')),'') AS name) tc
        WHERE t.is_primary_key = 0 AND t.data_space_id = 1
        --WHERE t.data_space_id > 1

DROP TABLE #Table

回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-12-22 09:02

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表