forked from bulentgucuk/DBA-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDisable Enable FK.sql
More file actions
39 lines (33 loc) · 1 KB
/
Disable Enable FK.sql
File metadata and controls
39 lines (33 loc) · 1 KB
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
DECLARE @RowId INT,
@Str VARCHAR(512)
DECLARE @DisableFK TABLE (
RowId INT IDENTITY(1,1),
FKName VARCHAR(255) NOT NULL,
FKTableName VARCHAR(100) NOT NULL,
ReferencedTable VARCHAR(100) NOT NULL,
Is_Disabled BIT NOT NULL
)
INSERT INTO @DisableFK
SELECT Name AS FKName,
OBJECT_NAME(Parent_Object_id) AS FKTableName,
OBJECT_NAME(Referenced_Object_id) AS ReferencedTable,
Is_Disabled
FROM SYS.FOREIGN_KEYS
WHERE OBJECT_NAME(Referenced_Object_id) = 'Applications'
ORDER BY OBJECT_NAME(Parent_Object_id)
SELECT @RowId = MAX(RowId) FROM @DisableFK
WHILE @RowId > 0
BEGIN
SELECT @Str = ''
SELECT @Str = 'ALTER TABLE ' + FKTableName + ' NOCHECK CONSTRAINT ' + FKName -- Disable foreign key constraint
--SELECT @Str = 'ALTER TABLE ' + FKTableName + ' CHECK CONSTRAINT ' + FKName -- Enable foreign key constraint
FROM @DisableFK
WHERE RowId = @RowId
PRINT @Str
--EXEC (@Str)
UPDATE @DisableFK
SET Is_Disabled = 1
WHERE RowId = @RowId
SELECT @RowId = @RowId - 1
END
SELECT * FROM @DisableFK