-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-ForeignKeys.sql
166 lines (147 loc) · 5.87 KB
/
5-ForeignKeys.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
--======================================= 5. Foreign Key =======================================
/*
This file holds the query for copying the reference database foreign keys to the destination database
To execute this, you need to replace "{Base}" with your reference database name
and replace "{Destination}" with your destination database name
This query creates a table called "#TempBase" in tempdb, so you need access to do that.
The query contains different parts like "adding new FK" and "deleting current FK".
*/
--********** 5.1 Add FK
Begin /*AddFK*/
If Object_ID ('tempdb.dbo.#TempBase', 'U') Is Not Null Drop Table #TempBase;
If Object_ID ('tempdb.dbo.#TempDestination', 'U') Is Not Null Drop Table #TempDestination;
Select obj.name AS FK_NAME,
sch.name AS [schema_name],
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
Into #TempBase
From {Base}.sys.foreign_key_columns fkc Inner Join
{Base}.sys.objects obj On obj.object_id = fkc.constraint_object_id Inner Join
{Base}.sys.tables tab1 On tab1.object_id = fkc.parent_object_id Inner Join
{Base}.sys.schemas sch On tab1.schema_id = sch.schema_id Inner Join
{Base}.sys.columns col1 On col1.column_id = parent_column_id And
col1.object_id = tab1.object_id Inner Join
{Base}.sys.tables tab2 On tab2.object_id = fkc.referenced_object_id Inner Join
{Base}.sys.columns col2 On col2.column_id = referenced_column_id And
col2.object_id = tab2.object_id
Select obj.name AS FK_NAME,
sch.name AS [schema_name],
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
Into #TempDestination
From {Destination}.sys.foreign_key_columns fkc Inner Join
{Destination}.sys.objects obj On obj.object_id = fkc.constraint_object_id Inner Join
{Destination}.sys.tables tab1 On tab1.object_id = fkc.parent_object_id Inner Join
{Destination}.sys.schemas sch On tab1.schema_id = sch.schema_id Inner Join
{Destination}.sys.columns col1 On col1.column_id = parent_column_id And
col1.object_id = tab1.object_id Inner Join
{Destination}.sys.tables tab2 On tab2.object_id = fkc.referenced_object_id Inner Join
{Destination}.sys.columns col2 On col2.column_id = referenced_column_id And
col2.object_id = tab2.object_id
Select *
From #TempBase
Where FK_NAME Not In
(Select Distinct FK_NAME From #TempDestination)
Declare @FknameAFK nvarchar(50)
Declare @TableNameAFK nvarchar(50)
Declare @rowAFK int
Set @rowAFK = 0
Declare Cursor_AddFK Cursor For
Select Distinct FK_NAME,[table]
From #TempBase
Where FK_NAME Not In
(Select FK_NAME From #TempDestination)
Open Cursor_AddFK
Fetch From Cursor_AddFK
Into @FknameAFK,@TableNameAFK
While @@Fetch_STATUS=0
Begin
Declare @AddFK NVARCHAR(MAX) = ''
Select @AddFK =
'ALTER TABLE ' + @TableNameAFK+
' ADD CONSTRAINT '+ @FknameAFK +' FOREIGN KEY ('+ c.[column]+
') REFERENCES '+c.referenced_table+'('+c.referenced_column+')'
From #TempBase as c
Where c.[table] = @TableNameAFK And
c.FK_NAME=@FknameAFK
PRINT @AddFK
--EXEC sys.sp_executesql @AddTable
Fetch Next From Cursor_AddFK
Into @FknameAFK,@TableNameAFK
END
Close Cursor_AddFK
Deallocate Cursor_AddFK
End /*AddFK*/
--********** 5.2 Delete FK
Begin /*DeleteFK*/
If Object_ID ('tempdb.dbo.#TempBase', 'U') Is Not Null Drop Table #TempBase;
If Object_ID ('tempdb.dbo.#TempDestination', 'U') Is Not Null Drop Table #TempDestination;
Select obj.name AS FK_NAME,
sch.name AS [schema_name],
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
Into #TempBase
From {Base}.sys.foreign_key_columns fkc Inner Join
{Base}.sys.objects obj On obj.object_id = fkc.constraint_object_id Inner Join
{Base}.sys.tables tab1 On tab1.object_id = fkc.parent_object_id Inner Join
{Base}.sys.schemas sch On tab1.schema_id = sch.schema_id Inner Join
{Base}.sys.columns col1 On col1.column_id = parent_column_id And
col1.object_id = tab1.object_id Inner Join
{Base}.sys.tables tab2 On tab2.object_id = fkc.referenced_object_id Inner Join
{Base}.sys.columns col2 On col2.column_id = referenced_column_id And
col2.object_id = tab2.object_id
Select obj.name AS FK_NAME,
sch.name AS [schema_name],
tab1.name AS [table],
col1.name AS [column],
tab2.name AS [referenced_table],
col2.name AS [referenced_column]
Into #TempDestination
From {Destination}.sys.foreign_key_columns fkc Inner Join
{Destination}.sys.objects obj On obj.object_id = fkc.constraint_object_id Inner Join
{Destination}.sys.tables tab1 On tab1.object_id = fkc.parent_object_id Inner Join
{Destination}.sys.schemas sch On tab1.schema_id = sch.schema_id Inner Join
{Destination}.sys.columns col1 On col1.column_id = parent_column_id And
col1.object_id = tab1.object_id Inner Join
{Destination}.sys.tables tab2 On tab2.object_id = fkc.referenced_object_id Inner Join
{Destination}.sys.columns col2 On col2.column_id = referenced_column_id And
col2.object_id = tab2.object_id
Select *
From #TempDestination
Where FK_NAME Not In
(Select Distinct FK_NAME From #TempBase)
Declare @FknameDFK nvarchar(50)
Declare @TableNameDFK nvarchar(50)
Declare @rowDFK int
Set @rowDFK = 0
Declare Cursor_DeleteFK Cursor For
Select Distinct FK_NAME,[table]
From #TempDestination
Where FK_NAME Not In
(Select FK_NAME From #TempBase)
Open Cursor_DeleteFK
Fetch From Cursor_DeleteFK
Into @FknameDFK,@TableNameDFK
While @@Fetch_STATUS=0
Begin
Declare @DeleteFK NVARCHAR(MAX) = ''
Select @DeleteFK =
'ALTER TABLE ' + @TableNameDFK +
' DROP CONSTRAINT '+ @FknameDFK
From #TempDestination as c
Where c.[table] = @TableNameDFK And
c.FK_NAME=@FknameDFK
PRINT @DeleteFK
--EXEC sys.sp_executesql @AddTable
Fetch Next From Cursor_DeleteFK
Into @FknameDFK,@TableNameDFK
End
Close Cursor_DeleteFK
Deallocate Cursor_DeleteFK
End /*DeleteFK*/