Files
chill_notes/OneNote/数据库知识库/工具脚本/批量创建app 表索引.md
2026-04-15 23:43:17 +08:00

800 B
Executable File

use safeMonitorApp
--声明一个变量
declare @tbname as nvarchar(100);
--声明一个游标用来遍历查询到的结果
declare C_userID CURSOR for
select distinct TABLE_NAME from information_schema.COLUMNS
where TABLE_NAME like '%App' --or TABLE_NAME like '%App'
--打开游标
open C_userID;
--获取游标指向的数据
fetch next from C_userID into @tbname;
while @@FETCH_STATUS = 0
BEGIN
--执行具体的操作
--创建非聚集索引
--create NONCLUSTERED INDEX 索引名称 ON 表名(字段名)
EXEC('create NONCLUSTERED INDEX '+'IX
'+@tbname+ ' ON '+@tbname+ '([deviceId] ASC,[CollectTime] DESC)')
--游标指向下一条数据
FETCH next from C_userID into @tbname;
END
--关闭游标
CLose C_userID
--释放游标
DEALLOCATE C_userID;