11
This commit is contained in:
11
全数据库创建脚本.txt
11
全数据库创建脚本.txt
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
-- 修正版全库ID字段索引创建脚本
|
-- 修正版全库ID字段索引创建脚本
|
||||||
BEGIN TRY
|
BEGIN TRY
|
||||||
BEGIN TRANSACTION;
|
|
||||||
|
|
||||||
-- 创建临时表存储需要创建索引的表和字段
|
-- 创建临时表存储需要创建索引的表和字段
|
||||||
CREATE TABLE #IndexCreationList (
|
CREATE TABLE #IndexCreationList (
|
||||||
@@ -11,12 +11,12 @@ BEGIN TRY
|
|||||||
|
|
||||||
-- 找出所有需要创建索引的字段
|
-- 找出所有需要创建索引的字段
|
||||||
INSERT INTO #IndexCreationList
|
INSERT INTO #IndexCreationList
|
||||||
SELECT
|
SELECT 10000
|
||||||
t.name AS TableName,
|
t.name AS TableName,
|
||||||
c.name AS ColumnName
|
c.name AS ColumnName
|
||||||
FROM sys.tables t
|
FROM sys.tables t
|
||||||
JOIN sys.columns c ON t.object_id = c.object_id
|
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 t.is_ms_shipped = 0
|
||||||
AND NOT EXISTS (
|
AND NOT EXISTS (
|
||||||
SELECT 1 FROM sys.indexes i
|
SELECT 1 FROM sys.indexes i
|
||||||
@@ -25,7 +25,8 @@ BEGIN TRY
|
|||||||
AND ic.column_id = c.column_id
|
AND ic.column_id = c.column_id
|
||||||
AND i.name = 'IX_' + t.name + '_' + c.name
|
AND i.name = 'IX_' + t.name + '_' + c.name
|
||||||
);
|
);
|
||||||
|
select * from #IndexCreationList;
|
||||||
|
|
||||||
-- 动态生成并执行创建索引的SQL
|
-- 动态生成并执行创建索引的SQL
|
||||||
DECLARE @sql NVARCHAR(MAX) = '';
|
DECLARE @sql NVARCHAR(MAX) = '';
|
||||||
SELECT @sql = @sql +
|
SELECT @sql = @sql +
|
||||||
@@ -56,7 +57,7 @@ BEGIN TRY
|
|||||||
ORDER BY t.name, i.name;
|
ORDER BY t.name, i.name;
|
||||||
|
|
||||||
DROP TABLE #IndexCreationList;
|
DROP TABLE #IndexCreationList;
|
||||||
COMMIT TRANSACTION;
|
|
||||||
END TRY
|
END TRY
|
||||||
BEGIN CATCH
|
BEGIN CATCH
|
||||||
IF @@TRANCOUNT > 0
|
IF @@TRANCOUNT > 0
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
USE AIS2025;
|
USE ;
|
||||||
|
|
||||||
IF Object_id('tempdb..#temp_table_info') <> 0
|
IF Object_id('tempdb..#temp_table_info') <> 0
|
||||||
DROP TABLE #temp_table_info
|
DROP TABLE #temp_table_info
|
||||||
|
|||||||
Reference in New Issue
Block a user