forked from ktaranov/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbo.sp_foreign_key$batch_drop_and_recreate.sql
183 lines (140 loc) · 6.64 KB
/
dbo.sp_foreign_key$batch_drop_and_recreate.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Author: Louis Davidson
Original link: http://sqlblog.com/blogs/louis_davidson/archive/2017/06/15/utility-to-temporarily-drop-foreign-key-constraints-on-a-set-of-tables.aspx
Desctiption: Utility to temporarily drop FOREIGN KEY constraints on a set of tables
*/
--Uses Object Created in Utility-ForeignKey$Scripting.sql, available here: https://www.dropbox.com/s/8hj1vfqpidb3d5i/Utility-ForeignKey%24Scripting.sql?dl=0
IF OBJECT_ID('utility.foreign_key$batch_drop', 'P') IS NULL
EXECUTE('CREATE PROCEDURE utility.foreign_key$batch_drop as SELECT 1');
GO
ALTER PROCEDURE utility.foreign_key$batch_drop
(
@table_Schema sysname = '%',
@table_Name sysname = '%',
@add_to_history_flag BIT = 0,
@force_replace_status VARCHAR(20) = 'AS_WAS' --ENABLED, UNTRUSTED, DISABLED
) AS
-- ----------------------------------------------------------------
-- Used to drop foreign keys, saving off what to recreate by batch name
--
-- 2017 Louis Davidson - drsql.org
-- ----------------------------------------------------------------
BEGIN
IF OBJECT_ID('Utility.foreign_key$batch_drop_toRestore') IS NULL
EXEC ('
CREATE TABLE Utility.foreign_key$batch_drop_toRestore
(
table_schema sysname NOT null,
table_name sysname NOT null,
constraint_name sysname NOT null,
recreate_script NVARCHAR(MAX) NOT null,
disabled_flag BIT NOT null,
trusted_flag bit NOT NULL
)
')
ELSE
IF @add_to_history_flag = 0
BEGIN
THROW 50000,'Parameter @add_to_history_flag set to only allow initialize case',1;
RETURN -100
END
set nocount on
declare @statements cursor
SET @statements = CURSOR FOR
WITH FK AS (
SELECT OBJECT_SCHEMA_NAME(parent_object_id) AS schemaName, OBJECT_NAME(parent_object_id) AS tableName,
NAME AS constraintName, foreign_keys.is_disabled AS disabledFlag,
IIF(foreign_keys.is_not_trusted = 1e,0,1) AS trustedFlag
FROM sys.foreign_keys
)
SELECT schemaName, tableName, constraintName, disabledFlag, FK.trustedFlag
FROM FK
WHERE schemaName LIKE @table_Schema
AND tableName LIKE @table_Name
ORDER BY schemaName, tableName, constraintName
OPEN @statements
DECLARE @statement VARCHAR(1000), @schemaName sysname, @tableName sysname, @constraintName sysname,
@constraintType sysname,@disabledFlag BIT, @trustedFlag BIT;
WHILE 1=1
BEGIN
FETCH FROM @statements INTO @schemaName, @tableName, @constraintName, @disabledFlag, @trustedFlag
IF @@FETCH_STATUS <> 0
BREAK
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Utility.foreign_key$batch_drop_toRestore (table_schema, table_name, constraint_name,
recreate_script, disabled_flag, trusted_flag)
SELECT @schemaName
, @tableName
, @constraintName
, utility.foreign_key$script(@schemaName, @constraintName,@force_replace_status) -- must be before the drop
, @disabledFlag
, @trustedFlag
SELECT @statement = 'ALTER TABLE ' + @schemaName + '.' + @tableName + ' DROP ' + @constraintName
EXEC (@statement)
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0
ROLLBACK
DECLARE @msg NVARCHAR(2000) =
CONCAT('Error occurred: ' , CAST(ERROR_NUMBER() AS VARCHAR(10)) , ':'
, ERROR_MESSAGE() , CHAR(13) , CHAR(10) ,
'Statement executed: ' , @statement);
THROW 50000,@msg,1;
END CATCH
END
END
GO
IF OBJECT_ID('utility.foreign_key$batch_recreate', 'P') IS NULL
EXECUTE('CREATE PROCEDURE utility.foreign_key$batch_recreate as SELECT 1');
GO
ALTER PROCEDURE utility.foreign_key$batch_recreate
AS
-- ----------------------------------------------------------------
-- Used to enable constraints
--
-- 2017 Louis Davidson - drsql.org
-- ----------------------------------------------------------------
BEGIN
IF OBJECT_ID('Utility.foreign_key$batch_drop_toRestore') IS NULL
BEGIN
THROW 50000,'Table Utility.foreign_key$batch_drop_toRestore must exist, as this is where the objects to resore are stored',1;
RETURN -100
END
set nocount on
declare @statements cursor
SET @statements = CURSOR FOR
SELECT table_schema AS schemaName, table_name AS tableName, constraint_name AS constraintName,
disabled_flag AS disabledFlag, recreate_script AS statement, trusted_flag
FROM Utility.foreign_key$batch_drop_toRestore
OPEN @statements
DECLARE @schemaName sysname, @tableName sysname, @constraintName sysname,
@disabledFlag BIT, @trustedFlag BIT, @codelocation VARCHAR(200), @statement NVARCHAR(MAX);
WHILE 1=1
BEGIN
FETCH FROM @statements INTO @schemaName, @tableName, @constraintName, @disabledFlag, @statement, @trustedFlag
IF @@FETCH_STATUS <> 0
BREAK
BEGIN TRY
BEGIN TRANSACTION
EXEC (@statement)
--PRINT @statement
DELETE FROM Utility.foreign_key$batch_drop_toRestore
WHERE table_schema = @schemaName
AND table_name = @tableName
AND constraint_name = @constraintName
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF XACT_STATE() = 0
ROLLBACK;
DECLARE @msg NVARCHAR(2000) =
'Error occurred: ' + CAST(ERROR_NUMBER() AS VARCHAR(10))+ ':' + ERROR_MESSAGE() + CHAR(13) + CHAR(10) +
'Statement executed: ' + @statement;
THROW 50000, @msg, 1;
END CATCH
END
DROP TABLE Utility.foreign_key$batch_drop_toRestore;
END
GO