-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
491 lines (423 loc) · 18.5 KB
/
schema.sql
File metadata and controls
491 lines (423 loc) · 18.5 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
if not exists (
select 1
from INFORMATION_SCHEMA.TABLES
where TABLE_NAME = 'TableGroup' and TABLE_SCHEMA ='dbo' and TABLE_TYPE = 'BASE TABLE')
begin
create table dbo.TableGroup
(
TableGroupId int identity(1,1) not null,
[Name] sysname not null,
SrcServerName sysname not null, -- source server name
SrcDatabaseName sysname not null, -- source database name
SrcConnectionOptions nvarchar(max) null, -- connection string options like ApplicationIntent, time out, user name, password etc
DstServerName sysname not null, -- destination server name
DstDatabaseName sysname not null, -- destination database name
DstConnectionOptions nvarchar(max) null, -- connection string options like ApplicationIntent, time out, user name, password etc
DisableFK bit not null -- disable FK before purge
)
alter table dbo.TableGroup add constraint PK_TableGroup primary key clustered (TableGroupId)
alter table dbo.TableGroup add constraint UQ_TableGroup__Name unique ([Name])
end
go
if not exists (
select 1
from INFORMATION_SCHEMA.TABLES
where TABLE_NAME = 'SourceTable' and TABLE_SCHEMA ='dbo' and TABLE_TYPE = 'BASE TABLE')
begin
create table dbo.SourceTable
(
SourceTableId int identity(1,1) not null,
TableGroupId int not null,
SchemaName sysname not null,
TableName sysname not null,
Active bit not null,
DataCopyBatchSize int not null, -- batch size for data copy
KeyCopyBatchSize int not null, -- batch size for keys copy
KeyQuery nvarchar(max) not null, -- to select primary keys values from source
Archive bit not null, -- can be archived
Purge bit not null, -- can be purged
PurgeOrder smallint not null, -- used to get purge sequence
DelayInterval char(8) not null, -- 'hh:mm:ss'
AlwaysRunCheck bit not null, -- always check for previously copied records
SrcWorkingTableName as SchemaName + '_' + TableName + '__src', -- working table name for source primary keys
DstWorkingTableName as SchemaName + '_' + TableName + '__dst', -- working table name for destination primary keys
WorkingTableKeyName as TableName + '__key',
WorkingTableFlagName as TableName + '__skip' -- 0 - ok, 1 - means row is duplicate and must be skipped
)
alter table dbo.SourceTable add constraint PK_SourceTable primary key clustered (SourceTableId)
alter table dbo.SourceTable add constraint UQ_SourceTable__TableGroupId_SchemaName_TableName unique (TableGroupId, SchemaName, TableName)
create nonclustered index IX_SourceTable__TableGroupId on dbo.SourceTable(TableGroupId)
alter table dbo.SourceTable add constraint FK_SourceTable_TableGroup__TableGroupId foreign key (TableGroupId)
references dbo.TableGroup (TableGroupId)
end
go
if not exists (
select 1
from INFORMATION_SCHEMA.TABLES
where TABLE_NAME = 'ProcessState' and TABLE_SCHEMA ='dbo' and TABLE_TYPE = 'BASE TABLE')
begin
create table dbo.ProcessState
(
ProcessStateId bigint identity(1,1) not null,
SourceTableId int not null,
CreateDate datetime not null,
KeyCopyDate datetime null, -- date of primary keys copy
KeyMaxValue int null,
LastArchivedKey int null, -- last copied surrogate primary key
LastArchivedDate datetime null, -- date of the last copy
RowsCopied int null, -- rows count
LastPurgedKey int null, -- last purged surrogate primary key
LastPurgedDate datetime null, -- date of the last purge
RowsPurged int null, -- rows count
CompleteDate datetime null
)
alter table dbo.ProcessState add constraint PK_ProcessState primary key clustered (ProcessStateId)
create nonclustered index IXF_ProcessState__SourceTableId ON dbo.ProcessState(SourceTableId) where CompleteDate is null
create nonclustered index IX_ProcessState__SourceTableId ON dbo.ProcessState(SourceTableId)
alter table dbo.ProcessState add constraint FK_ProcessState_SourceTable__SourceTableId foreign key (SourceTableId)
references dbo.SourceTable (SourceTableId)
end
go
if not exists (select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'stp_InsertProcessState' and ROUTINE_SCHEMA = 'dbo' and ROUTINE_TYPE = 'PROCEDURE')
begin
exec sp_executesql N'create procedure dbo.stp_InsertProcessState as select ''Fake procedure to be replaced by alter script'''
end
go
alter procedure dbo.stp_InsertProcessState
@SourceTableId int
as
set nocount on
insert dbo.ProcessState(SourceTableId, CreateDate)
values (@SourceTableId, getdate())
select cast(scope_identity() as bigint) ProcessStateId
go
if not exists (select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'stp_GetTableGroup' and ROUTINE_SCHEMA = 'dbo' and ROUTINE_TYPE = 'PROCEDURE')
begin
exec sp_executesql N'create procedure dbo.stp_GetTableGroup as select ''Fake procedure to be replaced by alter script'''
end
go
alter procedure dbo.stp_GetTableGroup
@Name sysname
as
set nocount on
select TableGroupId,
SrcServerName,
SrcDatabaseName,
SrcConnectionOptions,
DstServerName,
DstDatabaseName,
DstConnectionOptions,
DisableFK
from dbo.TableGroup
where [Name] = @Name
go
if not exists (select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'stp_GetSourceTable' and ROUTINE_SCHEMA = 'dbo' and ROUTINE_TYPE = 'PROCEDURE')
begin
exec sp_executesql N'create procedure dbo.stp_GetSourceTable as select ''Fake procedure to be replaced by alter script'''
end
go
alter procedure dbo.stp_GetSourceTable
@TableGroupId int
as
set nocount on
select SourceTableId,
SchemaName,
TableName,
DataCopyBatchSize,
KeyCopyBatchSize,
KeyQuery,
Archive,
Purge,
PurgeOrder,
DelayInterval,
AlwaysRunCheck,
SrcWorkingTableName,
DstWorkingTableName,
WorkingTableKeyName,
WorkingTableFlagName
from dbo.SourceTable
where Active = 1 and TableGroupId = @TableGroupId
go
if not exists (select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'stp_GetIncompleteProcessState' and ROUTINE_SCHEMA = 'dbo' and ROUTINE_TYPE = 'PROCEDURE')
begin
exec sp_executesql N'create procedure dbo.stp_GetIncompleteProcessState as select ''Fake procedure to be replaced by alter script'''
end
go
alter procedure dbo.stp_GetIncompleteProcessState
@SourceTableId int
as
set nocount on
select top 1 ProcessStateId,
CreateDate,
KeyCopyDate,
KeyMaxValue,
LastArchivedKey,
LastArchivedDate,
RowsCopied,
LastPurgedKey,
LastPurgedDate,
RowsPurged
from dbo.ProcessState
where SourceTableId = @SourceTableId and CompleteDate is null
order by ProcessStateId desc
go
if not exists (select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'stp_UpdateProcessState' and ROUTINE_SCHEMA = 'dbo' and ROUTINE_TYPE = 'PROCEDURE')
begin
exec sp_executesql N'create procedure dbo.stp_UpdateProcessState as select ''Fake procedure to be replaced by alter script'''
end
go
alter procedure dbo.stp_UpdateProcessState
@ProcessStateId bigint,
@KeyCopyDate datetime = null,
@KeyMaxValue int = null,
@LastArchivedKey int = null,
@LastArchivedDate datetime = null,
@RowsCopied int = null,
@LastPurgedKey int = null,
@LastPurgedDate datetime = null,
@RowsPurged int = null,
@CompleteDate datetime = null
as
set nocount on
update dbo.ProcessState
set KeyCopyDate = isnull(@KeyCopyDate, KeyCopyDate),
KeyMaxValue = isnull(@KeyMaxValue, KeyMaxValue),
LastArchivedKey = isnull(@LastArchivedKey, LastArchivedKey),
LastArchivedDate = isnull(@LastArchivedDate, LastArchivedDate),
RowsCopied = isnull(@RowsCopied, RowsCopied),
LastPurgedKey = isnull(@LastPurgedKey, LastPurgedKey),
LastPurgedDate = isnull(@LastPurgedDate, LastPurgedDate),
RowsPurged = isnull(@RowsPurged, RowsPurged),
CompleteDate = isnull(@CompleteDate, CompleteDate)
where ProcessStateId = @ProcessStateId
go
if not exists (select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'stp_GetBulkCopyData' and ROUTINE_SCHEMA = 'dbo' and ROUTINE_TYPE = 'PROCEDURE')
begin
exec sp_executesql N'create procedure dbo.stp_GetBulkCopyData as select ''Fake procedure to be replaced by alter script'''
end
go
alter procedure dbo.stp_GetBulkCopyData
@ProcessStateId bigint
as
set nocount on
declare @SrcDatabaseName sysname, @SchemaName sysname, @TableName sysname, @SrcWorkingTableName sysname
declare @WorkingTableKeyName sysname, @WorkingTableFlagName sysname
declare @DataCopyBatchSize int, @LastArchivedKey int, @Query nvarchar(max)
declare @SelectColumns nvarchar(max), @JoinColumns nvarchar(max)
select @SrcDatabaseName = gr.SrcDatabaseName,
@SchemaName = ta.SchemaName,
@TableName = ta.TableName,
@DataCopyBatchSize = ta.[DataCopyBatchSize],
@SrcWorkingTableName = ta.SrcWorkingTableName,
@WorkingTableKeyName = ta.WorkingTableKeyName,
@WorkingTableFlagName = ta.WorkingTableFlagName,
@LastArchivedKey = isnull(st.LastArchivedKey, 0)
from dbo.ProcessState st
join dbo.SourceTable ta
on ta.SourceTableId = st.SourceTableId
join dbo.TableGroup gr
on gr.TableGroupId = ta.TableGroupId
where st.ProcessStateId = @ProcessStateId
-- get table columns, without computed columns
set @Query = 'set @SelectColumns = (
select ''so.['' + COLUMN_NAME + ''], ''
from [' + @SrcDatabaseName + '].INFORMATION_SCHEMA.COLUMNS co
where TABLE_SCHEMA = ''' + @SchemaName + ''' and TABLE_NAME = ''' + @TableName + '''
and not exists
(
select 1
from [' + @SrcDatabaseName + '].sys.columns sc
where sc.[object_id] = object_id(''' + @SrcDatabaseName + '.'' + co.TABLE_SCHEMA + ''.'' + co.TABLE_NAME) and sc.[name] = co.COLUMN_NAME and sc.is_computed = 1
)
for xml path('''')
)'
exec sp_executesql @Query, N'@SelectColumns nvarchar(max) output', @SelectColumns = @SelectColumns output
set @SelectColumns = left(@SelectColumns, len(@SelectColumns) - 1)
-- get primary key columns for join
set @Query = 'set @JoinColumns = (
select ''wo.['' + ccu.COLUMN_NAME + ''] = so.['' + ccu.COLUMN_NAME + ''] and ''
from [' + @SrcDatabaseName + '].INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
join [' + @SrcDatabaseName + '].INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
on ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME and ccu.TABLE_NAME = tc.TABLE_NAME and ccu.TABLE_SCHEMA = tc.TABLE_SCHEMA
where tc.TABLE_SCHEMA = ''' + @SchemaName + ''' and tc.TABLE_NAME = ''' + @TableName + '''
and tc.CONSTRAINT_TYPE = ''PRIMARY KEY''
for xml path('''')
)'
exec sp_executesql @Query, N'@JoinColumns nvarchar(max) output', @JoinColumns = @JoinColumns output
set @JoinColumns = left(@JoinColumns, len(@JoinColumns) - 4)
set @Query = 'select ' + @SelectColumns + '
from [' + @SrcDatabaseName + '].[' + @SchemaName + '].[' + @TableName + '] so
join
(
select top(@DataCopyBatchSize) t.*
from dbo.[' + @SrcWorkingTableName + '] t
where t.[' + @WorkingTableKeyName + '] > @LastArchivedKey and [' + @WorkingTableFlagName + '] = 0
order by t.[' + @WorkingTableKeyName + ']
) wo
on ' + @JoinColumns
exec sp_executesql @Query, N'@DataCopyBatchSize int, @LastArchivedKey int', @DataCopyBatchSize = @DataCopyBatchSize, @LastArchivedKey = @LastArchivedKey
go
if not exists (select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'stp_PurgeData' and ROUTINE_SCHEMA = 'dbo' and ROUTINE_TYPE = 'PROCEDURE')
begin
exec sp_executesql N'create procedure dbo.stp_PurgeData as select ''Fake procedure to be replaced by alter script'''
end
go
alter procedure dbo.stp_PurgeData
@ProcessStateId bigint
as
set nocount on
declare @SrcDatabaseName sysname, @SchemaName sysname, @TableName sysname, @SrcWorkingTableName sysname
declare @WorkingTableKeyName sysname, @WorkingTableFlagName sysname
declare @DataCopyBatchSize int, @LastPurgedKey int, @Query nvarchar(max)
declare @JoinColumns nvarchar(max)
select @SrcDatabaseName = gr.SrcDatabaseName,
@SchemaName = ta.SchemaName,
@TableName = ta.TableName,
@DataCopyBatchSize = ta.[DataCopyBatchSize],
@SrcWorkingTableName = ta.SrcWorkingTableName,
@WorkingTableKeyName = ta.WorkingTableKeyName,
@WorkingTableFlagName = ta.WorkingTableFlagName,
@LastPurgedKey = isnull(st.LastPurgedKey, 0)
from dbo.ProcessState st
join dbo.SourceTable ta
on ta.SourceTableId = st.SourceTableId
join dbo.TableGroup gr
on gr.TableGroupId = ta.TableGroupId
where st.ProcessStateId = @ProcessStateId
-- get primary key columns for join
set @Query = 'set @JoinColumns = (
select ''so.['' + ccu.COLUMN_NAME + ''] = wo.['' + ccu.COLUMN_NAME + ''] and ''
from [' + @SrcDatabaseName + '].INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
join [' + @SrcDatabaseName + '].INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
on ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME and ccu.TABLE_NAME = tc.TABLE_NAME and ccu.TABLE_SCHEMA = tc.TABLE_SCHEMA
where tc.TABLE_SCHEMA = ''' + @SchemaName + ''' and tc.TABLE_NAME = ''' + @TableName + '''
and tc.CONSTRAINT_TYPE = ''PRIMARY KEY''
for xml path('''')
)'
exec sp_executesql @Query, N'@JoinColumns nvarchar(max) output', @JoinColumns = @JoinColumns output
set @JoinColumns = left(@JoinColumns, len(@JoinColumns) - 4)
set @Query = 'delete so
from [' + @SrcDatabaseName + '].[' + @SchemaName + '].[' + @TableName + '] so
join
(
select top(@DataCopyBatchSize) t.*
from dbo.[' + @SrcWorkingTableName + '] t
where t.[' + @WorkingTableKeyName + '] > @LastPurgedKey
order by t.[' + @WorkingTableKeyName + ']
) wo
on ' + @JoinColumns + '
select @@rowcount RowsPurgedForBatch'
exec sp_executesql @Query, N'@DataCopyBatchSize int, @LastPurgedKey int', @DataCopyBatchSize = @DataCopyBatchSize, @LastPurgedKey = @LastPurgedKey
go
if not exists (select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'stp_UpdateKeyMaxValue' and ROUTINE_SCHEMA = 'dbo' and ROUTINE_TYPE = 'PROCEDURE')
begin
exec sp_executesql N'create procedure dbo.stp_UpdateKeyMaxValue as select ''Fake procedure to be replaced by alter script'''
end
go
alter procedure dbo.stp_UpdateKeyMaxValue
@ProcessStateId bigint
as
set nocount on
declare @SchemaName sysname, @TableName sysname, @SrcWorkingTableName sysname
declare @WorkingTableKeyName sysname, @KeyMaxValue int
declare @Query nvarchar(max)
select @SchemaName = ta.SchemaName,
@TableName = ta.TableName,
@SrcWorkingTableName = ta.SrcWorkingTableName,
@WorkingTableKeyName = ta.WorkingTableKeyName
from dbo.ProcessState ast
join dbo.SourceTable ta
on ta.SourceTableId = ast.SourceTableId
join dbo.TableGroup gr
on gr.TableGroupId = ta.TableGroupId
where ast.ProcessStateId = @ProcessStateId
set @Query = 'select @KeyMaxValue = isnull(max([' + @WorkingTableKeyName + ']), 0) from dbo.[' + @SrcWorkingTableName + ']'
exec sp_executesql @Query, N'@KeyMaxValue int output', @KeyMaxValue = @KeyMaxValue output
exec dbo.stp_UpdateProcessState @ProcessStateId = @ProcessStateId, @KeyMaxValue = @KeyMaxValue
select @KeyMaxValue KeyMaxValue
go
if not exists (select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'stp_FixLastArchivedKey' and ROUTINE_SCHEMA = 'dbo' and ROUTINE_TYPE = 'PROCEDURE')
begin
exec sp_executesql N'create procedure dbo.stp_FixLastArchivedKey as select ''Fake procedure to be replaced by alter script'''
end
go
alter procedure dbo.stp_FixLastArchivedKey
@ProcessStateId bigint
as
set nocount on
declare @SchemaName sysname, @TableName sysname, @SrcWorkingTableName sysname, @DstWorkingTableName sysname
declare @WorkingTableKeyName sysname, @WorkingTableFlagName sysname, @LastArchivedKey bigint, @KeyMaxValue int
declare @Query nvarchar(max), @JoinColumns nvarchar(max)
select @SchemaName = ta.SchemaName,
@TableName = ta.TableName,
@SrcWorkingTableName = ta.SrcWorkingTableName,
@DstWorkingTableName = ta.DstWorkingTableName,
@WorkingTableKeyName = ta.WorkingTableKeyName,
@WorkingTableFlagName = ta.WorkingTableFlagName,
@KeyMaxValue = ast.KeyMaxValue
from dbo.ProcessState ast
join dbo.SourceTable ta
on ta.SourceTableId = ast.SourceTableId
where ast.ProcessStateId = @ProcessStateId
set @Query = 'set @JoinColumns = (
select ''de.['' + co.COLUMN_NAME + ''] = so.['' + co.COLUMN_NAME + ''] and ''
from INFORMATION_SCHEMA.COLUMNS co
where TABLE_SCHEMA = ''dbo'' and TABLE_NAME = ''' + @SrcWorkingTableName + '''
and co.COLUMN_NAME not in (''' + @WorkingTableKeyName + ''', ''' + @WorkingTableFlagName + ''')
for xml path('''')
)'
exec sp_executesql @Query, N'@JoinColumns nvarchar(max) output', @JoinColumns = @JoinColumns output
set @JoinColumns = left(@JoinColumns, len(@JoinColumns) - 4)
set @Query = 'update so
set [' + @WorkingTableFlagName + '] = 1
from dbo.[' + @SrcWorkingTableName + '] so
join dbo.[' + @DstWorkingTableName + '] de
on ' + @JoinColumns + '
select @LastArchivedKey = min([' + @WorkingTableKeyName + ']) - 1
from dbo.[' + @SrcWorkingTableName + '] so
where [' + @WorkingTableFlagName + '] = 0'
exec sp_executesql @Query, N'@LastArchivedKey bigint output', @LastArchivedKey = @LastArchivedKey output
set @LastArchivedKey = isnull(@LastArchivedKey, @KeyMaxValue)
exec dbo.stp_UpdateProcessState @ProcessStateId = @ProcessStateId, @LastArchivedKey = @LastArchivedKey
select @LastArchivedKey LastArchivedKey
go
if not exists (select 1 from INFORMATION_SCHEMA.ROUTINES where ROUTINE_NAME = 'stp_DisableEnableFK' and ROUTINE_SCHEMA = 'dbo' and ROUTINE_TYPE = 'PROCEDURE')
begin
exec sp_executesql N'create procedure dbo.stp_DisableEnableFK as select ''Fake procedure to be replaced by alter script'''
end
go
alter procedure dbo.stp_DisableEnableFK
@ProcessStateId bigint,
@Disable bit
as
set nocount on
declare @SrcDatabaseName sysname, @SchemaName sysname, @TableName sysname
declare @Query nvarchar(max), @AlterFK nvarchar(max)
select @SrcDatabaseName = gr.SrcDatabaseName,
@SchemaName = ta.SchemaName,
@TableName = ta.TableName
from dbo.ProcessState st
join dbo.SourceTable ta
on ta.SourceTableId = st.SourceTableId
join dbo.TableGroup gr
on gr.TableGroupId = ta.TableGroupId
where st.ProcessStateId = @ProcessStateId
set @Query = 'set @AlterFK = (
select case when @Disable = 1 then ''alter table [' + @SrcDatabaseName + '].['' + pa.TABLE_SCHEMA + ''].['' + pa.TABLE_NAME + ''] nocheck constraint ['' + pa.CONSTRAINT_NAME + ''];''
else ''alter table [' + @SrcDatabaseName + '].['' + pa.TABLE_SCHEMA + ''].['' + pa.TABLE_NAME + ''] with nocheck check constraint ['' + pa.CONSTRAINT_NAME + ''];''
end
from [' + @SrcDatabaseName + '].INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS fk
join [' + @SrcDatabaseName + '].INFORMATION_SCHEMA.KEY_COLUMN_USAGE pa
on pa.CONSTRAINT_SCHEMA = fk.CONSTRAINT_SCHEMA and pa.CONSTRAINT_NAME = fk.CONSTRAINT_NAME
join [' + @SrcDatabaseName + '].INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS ref
on ref.CONSTRAINT_SCHEMA = fk.UNIQUE_CONSTRAINT_SCHEMA and ref.CONSTRAINT_NAME = fk.UNIQUE_CONSTRAINT_NAME
and ref.ORDINAL_POSITION = pa.ORDINAL_POSITION
where ref.TABLE_SCHEMA = ''' + @SchemaName + ''' and ref.TABLE_NAME = ''' + @TableName + '''
for xml path('''')
)'
exec sp_executesql @Query, N'@AlterFK nvarchar(max) output, @Disable bit', @AlterFK = @AlterFK output, @Disable = @Disable
if @AlterFK is not null
begin
exec sp_executesql @AlterFK
end
go