This commit is contained in:
yuyubohh
2025-09-16 23:17:43 +08:00
parent ab3c84959c
commit 1537d24a34
2 changed files with 7 additions and 6 deletions

View File

@@ -1,7 +1,7 @@
-- 修正版全库ID字段索引创建脚本
BEGIN TRY
BEGIN TRANSACTION;
-- 创建临时表存储需要创建索引的表和字段
CREATE TABLE #IndexCreationList (
@@ -11,12 +11,12 @@ BEGIN TRY
-- 找出所有需要创建索引的字段
INSERT INTO #IndexCreationList
SELECT
SELECT 10000
t.name AS TableName,
c.name AS ColumnName
FROM sys.tables t
JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.name LIKE '%ID'
WHERE c.name LIKE '%ID' and t.name not like 'tmp%'
AND t.is_ms_shipped = 0
AND NOT EXISTS (
SELECT 1 FROM sys.indexes i
@@ -25,6 +25,7 @@ BEGIN TRY
AND ic.column_id = c.column_id
AND i.name = 'IX_' + t.name + '_' + c.name
);
select * from #IndexCreationList;
-- 动态生成并执行创建索引的SQL
DECLARE @sql NVARCHAR(MAX) = '';
@@ -56,7 +57,7 @@ BEGIN TRY
ORDER BY t.name, i.name;
DROP TABLE #IndexCreationList;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0

View File

@@ -1,5 +1,5 @@
USE AIS2025;
USE ;
IF Object_id('tempdb..#temp_table_info') <> 0
DROP TABLE #temp_table_info