-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessTables.sql
More file actions
executable file
·151 lines (123 loc) · 5.93 KB
/
ProcessTables.sql
File metadata and controls
executable file
·151 lines (123 loc) · 5.93 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
if object_id('dbo.ProcessTables') is not null
begin
exec ('drop procedure dbo.ProcessTables')
end
go
create procedure ProcessTables
@TargetDatabaseName varchar(128)=null
,@TargetTableName varchar(128)=null
,@SourceDatabaseName varchar(128)=null
,@SourceTableName varchar(128)=null
,@ProcessMode varchar(32)=null
,@TableGroup varchar(32)=null
,@Debug bit=0
as
begin
set nocount on
declare
@lTargetDatabaseName varchar(128)
,@lTargetSchemaName varchar(128)
,@lTargetTableName varchar(128)
,@lProcessMode varchar(32)
,@Msg varchar(max)
,@StoredProcName varchar(255)
,@CurrentOperation varchar(255)
,@ErrorFlag bit = 0
,@ErrorMessage varchar(max)
/*
This sproc iterates over table, syncing, diffing, or reconciling them one at a time
Order of operations:
- Load the tables to process into a temp table
- Using a while loop, call either Diff, Sync, or Reconcile
*/
set @ErrorMessage = ''
set @StoredProcName = object_name(@@procid)
set @Msg = 'Starting ' + @StoredProcName + ', filters:'
+' @TableGroup=' + isnull(@TableGroup,'null')
+', @TargetDatabase=' + isnull(@TargetDatabaseName,'null')
+', @TargetTable=' + isnull(@TargetTableName,'null')
+', @SourceDatabase Name=' + isnull(@SourceDatabaseName,'null')
+', @SourceTableName=' + isnull(@SourceTableName,'null')
+', @ProcessMode=' + isnull(@ProcessMode,'null')
+', @Debug=' + isnull(convert(varchar,@Debug),'null')
exec dbo.WriteLog @ProcName=@StoredProcName, @MessageText=@Msg, @Status='Starting'
if object_id('tempdb..#tablesToProcess') is not null
begin
exec ('drop table #tablesToProcess');
end
select
s.TargetDatabase
,s.TargetSchema
,s.TargetTable
,s.ProcessMode
into #tablesToProcess
from dbo.SyncConfig s
where 1=1
and (s.TargetDatabase = @TargetDatabaseName or @TargetDatabaseName is null)
and (s.TargetTable = @TargetTableName or @TargetTableName is null)
and (s.SourceDatabase = @SourceDatabaseName or @SourceDatabaseName is null)
and (s.SourceTable = @SourceTableName or @SourceTableName is null)
and (s.TableGroup = @TableGroup or @TableGroup is null)
and (s.ProcessMode = @ProcessMode or @ProcessMode is null
or (@ProcessMode='Reconcile' and ReconcileTable is not null) )
and s.IsActive = 1
while exists (select * from #tablesToProcess)
begin
select top 1
@lTargetDatabaseName = t.TargetDatabase
,@lTargetSchemaName = t.TargetSchema
,@lTargetTableName = t.TargetTable
,@lProcessMode = t.ProcessMode
from #tablesToProcess t
begin try
if @lProcessMode = 'Type-2'
begin
set @CurrentOperation = 'DiffTable: Target='+@lTargetDatabaseName+'.'+@lTargetSchemaName+'.'+@lTargetTableName
set @Msg = 'Starting ' + @CurrentOperation
+', @Debug=' + isnull(convert(varchar,@Debug),'null')
exec dbo.WriteLog @ProcName=@StoredProcName, @MessageText=@Msg, @Status='Calling DiffTable'
exec dbo.DiffTable @TargetDatabaseName=@lTargetDatabaseName, @TargetSchemaName=@lTargetSchemaName, @TargetTableName = @lTargetTableName, @Debug = @Debug
end
if @lProcessMode = 'Type-1'
begin
set @CurrentOperation = 'SyncTable: Target='+@lTargetDatabaseName+'.'+@lTargetSchemaName+'.'+@lTargetTableName
set @Msg = 'Starting ' + @CurrentOperation
+', @Debug=' + isnull(convert(varchar,@Debug),'null')
exec dbo.WriteLog @ProcName=@StoredProcName, @MessageText=@Msg,@Status='Calling SyncTable'
exec dbo.SyncTable @TargetDatabaseName=@lTargetDatabaseName, @TargetSchemaName=@lTargetSchemaName, @TargetTableName = @lTargetTableName, @Debug = @Debug
end
if @ProcessMode = 'Reconcile'
begin
set @CurrentOperation = 'ReconcileTable: Target='+@lTargetDatabaseName+'.'+@lTargetSchemaName+'.'+@lTargetTableName
set @Msg = 'Starting ' + @CurrentOperation
+', @Debug=' + isnull(convert(varchar,@Debug),'null')
exec dbo.WriteLog @ProcName=@StoredProcName, @MessageText=@Msg,@Status='Calling ReconcileTable'
exec dbo.ReconcileTable @TargetDatabaseName=@lTargetDatabaseName, @TargetSchemaName=@lTargetSchemaName, @TargetTableName = @lTargetTableName, @Debug = @Debug
end
end try
begin catch
select @ErrorMessage = @ErrorMessage + cast(getdate() as varchar) + CHAR(13) + CHAR(10)
+ error_message() + CHAR(13) + CHAR(10) + CHAR(13) + CHAR(10)
set @Msg = 'ERROR in '+ @CurrentOperation + '. Review previous log entries for details.'
exec dbo.WriteLog @ProcName=@StoredProcName, @Status='ERROR'
,@MessageText=@Msg
set @ErrorFlag = 1
end catch
delete from #tablesToProcess
where TargetDatabase = @lTargetDatabaseName
and TargetSchema = @lTargetSchemaName
and TargetTable = @lTargetTableName
end
set @Msg = 'Finished ' + @StoredProcName + ', filters:'
+' @TableGroup=' + isnull(@TableGroup,'null')
+', @TargetDatabase=' + isnull(@TargetDatabaseName,'null')
+', @TargetTable=' + isnull(@TargetTableName,'null')
+', @SourceDatabase Name=' + isnull(@SourceDatabaseName,'null')
+', @SourceTableName=' + isnull(@SourceTableName,'null')
+', @ProcessMode=' + isnull(@ProcessMode,'null')
+', @Debug=' + isnull(convert(varchar,@Debug),'null')
exec dbo.WriteLog @ProcName=@StoredProcName, @MessageText=@Msg, @Status='Finished'
if @ErrorFlag = 1
raiserror (@ErrorMessage, 16, 1)
end
go