-
Notifications
You must be signed in to change notification settings - Fork 2
/
Data_Archiving_And_Purging_Stable.sql
729 lines (561 loc) · 25.2 KB
/
Data_Archiving_And_Purging_Stable.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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
CREATE PROCEDURE FindMissingColumns_with_datatype_and_update_1
@source_table_name NVARCHAR(255),
@destination_table_name NVARCHAR(255)
AS
BEGIN
-- Create a temporary table to store column names and data types
CREATE TABLE #temp_columns (column_name NVARCHAR(255), data_type NVARCHAR(50));
-- Get the column names and data types from the source table
INSERT INTO #temp_columns (column_name, data_type)
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @source_table_name;
-- Remove column names that exist in the destination table
DECLARE @destination_db NVARCHAR(255);
DECLARE @destination_table NVARCHAR(255);
SET @destination_db = PARSENAME(@destination_table_name, 3);
SET @destination_table = PARSENAME(@destination_table_name, 1);
DECLARE @sql NVARCHAR(MAX);
SET @sql = 'DELETE FROM #temp_columns WHERE column_name IN (SELECT COLUMN_NAME FROM ' + @destination_db + '.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ''' + @destination_table + ''')';
EXEC(@sql);
-- Select the remaining columns (missing in destination) along with their data types
SELECT column_name, data_type FROM #temp_columns;
DECLARE @column_name NVARCHAR(255);
DECLARE @data_type NVARCHAR(50);
-- ... previous code ...
DECLARE alter_cursor CURSOR FOR
SELECT column_name, data_type FROM #temp_columns;
OPEN alter_cursor;
FETCH NEXT FROM alter_cursor INTO @column_name, @data_type;
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @alter_sql NVARCHAR(MAX);
SET @alter_sql = 'ALTER TABLE ' + @destination_table_name + ' ADD ' + @column_name + ' ' + @data_type;
EXEC(@alter_sql);
FETCH NEXT FROM alter_cursor INTO @column_name, @data_type;
END
CLOSE alter_cursor;
DEALLOCATE alter_cursor;
-- ... rest of the code ...
-- Drop the temporary table
DROP TABLE #temp_columns;
END;
CREATE PROCEDURE Find_Extra_Columns_Modified_2
@source_table_name NVARCHAR(255),
@destination_table_name NVARCHAR(255),
@ColumnList NVARCHAR(MAX) OUTPUT
AS
BEGIN
-- Create temporary tables to store column names
CREATE TABLE #temp_columns (column_name NVARCHAR(255));
CREATE TABLE #temp_columns2 (column_name NVARCHAR(255));
DECLARE @source_db NVARCHAR(255);
SET @source_db = PARSENAME(@source_table_name, 3);
-- Get the column names from the source table
DECLARE @source_query NVARCHAR(MAX);
SET @source_query = 'INSERT INTO #temp_columns (column_name) SELECT COLUMN_NAME FROM ' + @source_db + '.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ''' + PARSENAME(@source_table_name, 1) + '''';
EXEC(@source_query);
SET @source_query = 'INSERT INTO #temp_columns2 (column_name) SELECT COLUMN_NAME FROM ' + @source_db + '.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ''' + PARSENAME(@source_table_name, 1) + '''';
EXEC(@source_query);
-- Debug: Print the column names from source table
PRINT 'Source Table Columns:';
SELECT column_name FROM #temp_columns;
-- Remove column names that exist in the destination table
DECLARE @destination_db NVARCHAR(255);
DECLARE @destination_table NVARCHAR(255);
SET @destination_db = PARSENAME(@destination_table_name, 3);
SET @destination_table = PARSENAME(@destination_table_name, 1);
DECLARE @sql NVARCHAR(MAX);
SET @sql = 'DELETE FROM #temp_columns WHERE column_name IN (SELECT COLUMN_NAME FROM ' + @destination_db + '.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ''' + @destination_table + ''')';
EXEC(@sql);
-- Debug: Print the remaining columns
PRINT 'Remaining Columns:';
SELECT column_name FROM #temp_columns;
SET @sql = 'DELETE FROM #temp_columns2 WHERE column_name IN (SELECT COLUMN_NAME FROM #temp_columns)';
EXEC(@sql);
-- Concatenate column names into @ColumnList
-- Concatenate column names into @ColumnList
SET @ColumnList = '';
SELECT @ColumnList = @ColumnList + ', ' + column_name
FROM #temp_columns2;
-- Remove leading comma and space
IF LEN(@ColumnList) > 2
SET @ColumnList = SUBSTRING(@ColumnList, 3, LEN(@ColumnList) - 2);
-- Drop the temporary tables
DROP TABLE #temp_columns;
DROP TABLE #temp_columns2;
END;
CREATE PROCEDURE FindChildTablesForParent_Working_5
@parent_table NVARCHAR(100),
@final_list NVARCHAR(MAX) OUTPUT,
@changed_list NVARCHAR(MAX) OUTPUT
AS
BEGIN
DECLARE @parent_primarykeys NVARCHAR(MAX);
SET @parent_primarykeys = '';
SET @final_list = '';
SET @changed_list = '';
SET @changed_list = @changed_list + QUOTENAME(@parent_table) + ' ';
-- Get the primary keys of the parent table
SELECT @parent_primarykeys = @parent_primarykeys + COLUMN_NAME + ', '
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON tc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME
WHERE tc.TABLE_NAME = @parent_table
AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY';
-- Find child tables dynamically
DECLARE @child_table NVARCHAR(100);
DECLARE @child_primarykeys NVARCHAR(MAX);
DECLARE child_cursor CURSOR FOR
SELECT
cu.TABLE_NAME,
(
SELECT COLUMN_NAME + ', '
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
WHERE kcu.TABLE_NAME = cu.TABLE_NAME
FOR XML PATH('')
) AS PrimaryKeys
FROM
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON rc.UNIQUE_CONSTRAINT_NAME = tc.CONSTRAINT_NAME
JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS cu ON rc.CONSTRAINT_NAME = cu.CONSTRAINT_NAME
WHERE
tc.TABLE_NAME = @parent_table;
OPEN child_cursor;
FETCH NEXT FROM child_cursor INTO @child_table, @child_primarykeys;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @final_list = @final_list + 'Child Table: ' + @child_table + CHAR(13) + CHAR(10);
SET @changed_list = @changed_list + @child_table + CHAR(13) + CHAR(10);
SET @final_list = @final_list + 'Primary Keys: ' + @child_primarykeys + CHAR(13) + CHAR(10);
-- Add your processing logic here
FETCH NEXT FROM child_cursor INTO @child_table, @child_primarykeys;
END
CLOSE child_cursor;
DEALLOCATE child_cursor;
-- Print the final list for debugging
END;
CREATE PROCEDURE GetPrimaryKeyInfo_6
@TableName NVARCHAR(255),
@PrimaryKeyColumnName NVARCHAR(255) OUTPUT,
@PrimaryKeyDataType NVARCHAR(50) OUTPUT,
@PrimaryKeyValues NVARCHAR(MAX) OUTPUT,
@PrimaryKeyCount INT OUTPUT
AS
BEGIN
DECLARE @PrimaryKeyConstraintName NVARCHAR(255);
-- Find the primary key column and constraint name
SELECT @PrimaryKeyColumnName = COLUMN_NAME,
@PrimaryKeyConstraintName = CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = @TableName
AND CONSTRAINT_NAME = (
SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME = @TableName
AND CONSTRAINT_TYPE = 'PRIMARY KEY'
);
IF @PrimaryKeyColumnName IS NOT NULL
BEGIN
-- Get the data type of the primary key column
SELECT @PrimaryKeyDataType = DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName
AND COLUMN_NAME = @PrimaryKeyColumnName;
SET @PrimaryKeyValues = '';
-- Dynamically generate SQL to retrieve primary key values
DECLARE @DynamicSQL NVARCHAR(MAX);
SET @DynamicSQL = '
SELECT @PrimaryKeyValues = COALESCE(@PrimaryKeyValues + '', '', '''') + CONVERT(NVARCHAR(MAX), ' + QUOTENAME(@PrimaryKeyColumnName) + ')
FROM ' + QUOTENAME(@TableName);
-- Execute dynamic SQL
EXEC sp_executesql @DynamicSQL, N'@PrimaryKeyValues NVARCHAR(MAX) OUTPUT', @PrimaryKeyValues OUTPUT;
-- Output primary key column information
PRINT 'Primary Key Column: ' + @PrimaryKeyColumnName;
PRINT 'Data Type: ' + @PrimaryKeyDataType;
PRINT 'Primary Key Values: ' + @PrimaryKeyValues;
-- Get the count of primary keys moved using dynamic SQL
DECLARE @CountSQL NVARCHAR(MAX);
SET @CountSQL = 'SELECT @PrimaryKeyCount = COUNT(*) FROM ' + QUOTENAME(@TableName);
EXEC sp_executesql @CountSQL, N'@PrimaryKeyCount INT OUTPUT', @PrimaryKeyCount OUTPUT;
-- Output count of primary keys moved
PRINT 'Number of Primary Keys Moved: ' + CAST(@PrimaryKeyCount AS NVARCHAR(MAX));
END
ELSE
BEGIN
PRINT 'No primary key found for the specified table.';
END
END;
DECLARE @result nvarchar(max);
DECLARE @result_cols nvarchar(max);
DECLARE @result_datatype nvarchar(max);
DECLARE @result_count int;
EXEC GetPrimaryKeyInfo_6 'ChildTable2', @result_cols OUTPUT, @result_datatype OUTPUT, @result OUTPUT, @result_count OUTPUT;
print @result;
print @result_cols;
print @result_datatype;
print @result_count;
CREATE PROCEDURE UPDATION_OF_DATA_PROPER_LOGS_FAULT_TOLERANCE_HEADER_CHILD_25
@SourceDatabase NVARCHAR(100),
@SourceSchema NVARCHAR(100),
@SourceTable NVARCHAR(100),
@TargetDatabase NVARCHAR(100),
@TargetSchema NVARCHAR(100),
@TargetTable NVARCHAR(100),
@condition NVARCHAR(MAX),
@LogsID INT OUTPUT,
@message NVARCHAR(MAX)
AS
BEGIN
DECLARE @SQL NVARCHAR(MAX);
DECLARE @ChildSQL NVARCHAR(MAX);
DECLARE @ChildTableName NVARCHAR(100);
-- Create temporary table to store child table names
CREATE TABLE #ChildTables (TableName NVARCHAR(100));
-- Find child tables
INSERT INTO #ChildTables
SELECT t.name
FROM sys.foreign_keys AS fk
INNER JOIN sys.tables AS t ON fk.parent_object_id = t.object_id
WHERE OBJECT_NAME(fk.referenced_object_id) = @SourceTable
AND SCHEMA_NAME(t.schema_id) = @SourceSchema
AND DB_NAME() = @SourceDatabase;
select * from #ChildTables;
DECLARE @StartTime DATETIME, @EndTime DATETIME;
SET @StartTime = GETDATE();
Create table #tempPrimary (logid int, childlogid int, tablename nvarchar(max), column_name nvarchar(max), datatype nvarchar(max), prim_keys nvarchar(max), no_of_prim int, columns_changed nvarchar(max));
DECLARE @Purpose NVARCHAR(MAX);
DECLARE @Status NVARCHAR(MAX);
DECLARE @DestinationTableNames NVARCHAR(MAX);
DECLARE @TablesChanged NVARCHAR(MAX);
DECLARE @SourceTableName NVARCHAR(MAX);
DECLARE @Message_Req NVARCHAR(MAX);
DECLARE @final nvarchar(MAX);
DECLARE @change nvarchar(MAX);
-- Creating a fix table if doesnt exists
SET @SQL = '
IF NOT EXISTS (SELECT 1 FROM ' + QUOTENAME(@TargetDatabase) + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' + QUOTENAME(@TargetSchema, '''') + ' AND TABLE_NAME = ''fix_table'')
BEGIN
CREATE TABLE ' + QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.fix_table (
LogsID INT IDENTITY(1,1) PRIMARY KEY,
LogMessage NVARCHAR(MAX),
CreationDateTime DATETIME DEFAULT GETDATE()
);
END;
';
EXEC(@SQL);
-- Creating a fix table 2 if doesnt exists
SET @SQL = '
IF NOT EXISTS (SELECT 1 FROM ' + QUOTENAME(@TargetDatabase) + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' + QUOTENAME(@TargetSchema, '''') + ' AND TABLE_NAME = ''fix_table_2'')
BEGIN
CREATE TABLE ' + QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.fix_table_2 (
Child_ID INT,
CreationDateTime DATETIME DEFAULT GETDATE()
);
USE ' + QUOTENAME(@TargetDatabase) + ';
INSERT INTO ' + QUOTENAME(@TargetSchema) + '.fix_table_2 (Child_ID)
VALUES (1);
END;
';
EXEC(@SQL);
-- Insert a record into fix_table and get the generated LogsID
SET @SQL = '
USE ' + QUOTENAME(@TargetDatabase) + ';
INSERT INTO ' + QUOTENAME(@TargetSchema) + '.fix_table (LogMessage)
VALUES (@message);
SET @LogsID = SCOPE_IDENTITY();';
EXEC sp_executesql @SQL, N'@message NVARCHAR(MAX), @LogsID INT OUTPUT', @message, @LogsID OUTPUT;
-- Create Header_Log_Table if it doesn't exist
SET @SQL = '
IF NOT EXISTS (SELECT 1 FROM ' + QUOTENAME(@TargetDatabase) + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' + QUOTENAME(@TargetSchema, '''') + ' AND TABLE_NAME = ''Header_Log_Table'')
BEGIN
CREATE TABLE ' + QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.Header_Log_Table (
LogID INT IDENTITY(1,1) PRIMARY KEY,
Purpose NVARCHAR(MAX),
SourceTableName NVARCHAR(MAX),
DestinationTableNames NVARCHAR(MAX),
TablesChanged NVARCHAR(MAX),
StartTime DATETIME,
EndTime DATETIME,
Status NVARCHAR(50),
);
END;
';
EXEC(@SQL);
-- Create Child_Log_Table if it doesn't exist
SET @SQL = '
IF NOT EXISTS (SELECT 1 FROM ' + QUOTENAME(@TargetDatabase) + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' + QUOTENAME(@TargetSchema, '''') + ' AND TABLE_NAME = ''Child_Log_Table'')
BEGIN
CREATE TABLE ' + QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.Child_Log_Table (
LogID INT,
Child_LogsID INT IDENTITY(1,1) PRIMARY KEY,
TableName NVARCHAR(MAX),
ColumnName NVARCHAR(MAX),
DataType NVARCHAR(MAX),
PrimaryKeysMoved NVARCHAR(MAX),
No_of_Keys_Moved int,
Columns_Changed NVARCHAR(MAX),
CreationDateTime DATETIME DEFAULT GETDATE()
);
END;
';
EXEC(@SQL);
BEGIN TRY
-- Check if target table exists, if yes, insert data, else create the table and add the data from the source
SET @SQL = '
IF EXISTS (SELECT 1 FROM ' + QUOTENAME(@TargetDatabase) + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' + QUOTENAME(@TargetSchema, '''') + ' AND TABLE_NAME = ''' + @TargetTable + ''')
BEGIN
USE ' + QUOTENAME(@TargetDatabase) + ';
INSERT INTO ' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(@TargetTable) + '
SELECT @LogsID AS LogsID, *
FROM ' + QUOTENAME(@SourceDatabase) + '.' + QUOTENAME(@SourceSchema) + '.' + QUOTENAME(@SourceTable) +
@condition + ';
END;
ELSE
BEGIN
USE ' + QUOTENAME(@TargetDatabase) + ';
SELECT @LogsID AS LogsID, *
INTO ' + QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(@TargetTable) + '
FROM ' + QUOTENAME(@SourceDatabase) + '.' + QUOTENAME(@SourceSchema) + '.' + QUOTENAME(@SourceTable) +
@condition + ';
END;
';
-- Pass @LogsID as parameter
EXEC sp_executesql @SQL, N'@LogsID INT', @LogsID;
-- Check if child table exists, if yes, Insert it
DECLARE childTablesCursor CURSOR FOR
SELECT TableName FROM #ChildTables;
OPEN childTablesCursor;
FETCH NEXT FROM childTablesCursor INTO @ChildTableName;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = '
DECLARE @pri_col_val NVARCHAR(MAX);
DECLARE @pri_col_datatype NVARCHAR(MAX);
DECLARE @prim_vals NVARCHAR(MAX);
DECLARE @prim_count NVARCHAR(MAX);
DECLARE @childid int;
DECLARE @childid_new int;
DECLARE @ChangesInColumn NVARCHAR(MAX); -- Declare @ChangesInColumn inside dynamic SQL
SELECT @childid = Child_ID FROM ' + QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME('fix_table_2') + ';
SET @childid_new = @childid + 1;
UPDATE ' + QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME('fix_table_2') + '
SET Child_ID = @childid_new
WHERE Child_ID = @childid;
EXEC GetPrimaryKeyInfo_6 ''' + @ChildTableName + ''',
@pri_col_val OUTPUT,
@pri_col_datatype OUTPUT,
@prim_vals OUTPUT,
@prim_count OUTPUT;
SET @ChangesInColumn = ''No Changes'';
INSERT INTO #tempPrimary (logid, childlogid, tablename, column_name, datatype, prim_keys, no_of_prim, columns_changed)
VALUES (@LogsID, @childid, ''' + QUOTENAME(@ChildTableName) + ''',
@pri_col_val, @pri_col_datatype, @prim_vals, @prim_count, @ChangesInColumn);
';
EXEC sp_executesql @SQL, N'@LogsID INT', @LogsID;
select * from #tempPrimary;
BEGIN TRY
SET @SQL = '
IF EXISTS (SELECT 1 FROM ' + QUOTENAME(@TargetDatabase) + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' + QUOTENAME(@TargetSchema, '''') + ' AND TABLE_NAME = ''' + @ChildTableName + ''')
BEGIN
USE ' + QUOTENAME(@TargetDatabase) + ';
INSERT INTO ' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(@ChildTableName) + '
SELECT @LogsID AS LogsID, *
FROM ' + QUOTENAME(@SourceDatabase) + '.' + QUOTENAME(@SourceSchema) + '.' + QUOTENAME(@ChildTableName) + @condition + ';
END;
ELSE
BEGIN
USE ' + QUOTENAME(@TargetDatabase) + ';
SELECT @LogsID AS LogsID, *
INTO ' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(@ChildTableName) + '
FROM ' + QUOTENAME(@SourceDatabase) + '.' + QUOTENAME(@SourceSchema) + '.' + QUOTENAME(@ChildTableName) + @condition + ';
END;
';
EXEC sp_executesql @SQL, N'@LogsID INT', @LogsID;
END TRY
BEGIN CATCH
BEGIN TRY
SET @message = 'Altering the Table Structure';
PRINT @message;
DECLARE @TargetTablePath NVARCHAR(1000);
SET @TargetTablePath = QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(@ChildTableName);
PRINT @ChildTableName;
EXEC FindMissingColumns_with_datatype_and_update_1 @ChildTableName, @TargetTablePath;
SET @SQL = '
IF EXISTS (SELECT 1 FROM ' + QUOTENAME(@TargetDatabase) + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' + QUOTENAME(@TargetSchema, '''') + ' AND TABLE_NAME = ''' + @ChildTableName + ''')
BEGIN
USE ' + QUOTENAME(@TargetDatabase) + ';
INSERT INTO ' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(@ChildTableName) + '
SELECT @LogsID AS LogsID, *
FROM ' + QUOTENAME(@SourceDatabase) + '.' + QUOTENAME(@SourceSchema) + '.' + QUOTENAME(@ChildTableName) + @condition + ';
END;
Update #tempPrimary set columns_changed = ''Column Removed'' where logid = @LogsID;
';
EXEC sp_executesql @SQL, N'@LogsID INT', @LogsID;
END TRY
BEGIN CATCH
DECLARE @TargetTablePath_Dest NVARCHAR(1000);
DECLARE @TargetTablePath_Source NVARCHAR(1000);
SET @TargetTablePath_Dest = QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(@ChildTableName);
SET @TargetTablePath_Source = QUOTENAME(@SourceDatabase) + '.' + QUOTENAME(@SourceSchema) + '.' + QUOTENAME(@ChildTableName);
DECLARE @result nvarchar(max);
EXEC Find_Extra_Columns_Modified_2 @TargetTablePath_Dest, @TargetTablePath_Source, @ColumnList = @result OUTPUT;
PRINT CONVERT(VARCHAR, @result);
SET @SQL = '
IF EXISTS (SELECT 1 FROM ' + QUOTENAME(@TargetDatabase) + '.INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' + QUOTENAME(@TargetSchema, '''') + ' AND TABLE_NAME = ''' + @ChildTableName + ''')
BEGIN
USE ' + QUOTENAME(@TargetDatabase) + ';
INSERT INTO ' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(@ChildTableName) + ' ('+'LogsID, '+ @result + ')
SELECT @LogsID AS LogsID, ' + @result + '
FROM ' + QUOTENAME(@SourceDatabase) + '.' + QUOTENAME(@SourceSchema) + '.' + QUOTENAME(@ChildTableName) + @condition + ';
END;
Update #tempPrimary set columns_changed = ''Column Added'' where logid = @LogsID;
';
PRINT 'Dynamic SQL Statement:';
PRINT @SQL;
EXEC sp_executesql @SQL, N'@LogsID INT', @LogsID;
-- Write the part here
END CATCH
END CATCH
FETCH NEXT FROM childTablesCursor INTO @ChildTableName;
END;
CLOSE childTablesCursor;
DEALLOCATE childTablesCursor;
-- Clean up temporary table
DROP TABLE #ChildTables;
SET @EndTime = GETDATE();
DECLARE @primaryKeysList3 NVARCHAR(255);
set @primaryKeysList3 = 'Need to find';
-- Insert a record into Header_Log_Table and get the generated LogsID
SET @SQL = '
USE ' + QUOTENAME(@TargetDatabase) + ';
INSERT INTO ' + QUOTENAME(@TargetSchema) + '.Header_Log_Table (
Purpose,
SourceTableName,
DestinationTableNames,
TablesChanged,
StartTime,
EndTime,
Status
)
VALUES (
@Purpose,
@SourceTableName,
@DestinationTableNames,
@TablesChanged,
@StartTime,
@EndTime,
@Status
);
';
SET @Purpose = @message;
SET @Status = 'Done';
SET @DestinationTableNames = @TargetDatabase +' . '+ @TargetSchema + ' . ' + @TargetTable;
SET @TablesChanged = 'Deciding';
SET @SourceTableName = @SourceTable;
SET @Message_Req = @message;
EXEC FindChildTablesForParent_Working_5 @parent_table = @SourceTable, @final_list = @final OUTPUT, @changed_list = @change OUTPUT;
EXEC sp_executesql @SQL, N'@Purpose NVARCHAR(MAX), @SourceTableName NVARCHAR(MAX), @DestinationTableNames NVARCHAR(MAX), @TablesChanged NVARCHAR(MAX), @StartTime DATETIME, @EndTime DATETIME, @Status NVARCHAR(50), @LogsID INT OUTPUT',
@Purpose, @SourceTableName, @DestinationTableNames, @change, @StartTime, @EndTime, @Status, @LogsID OUTPUT;
DECLARE @MailBody NVARCHAR(MAX);
SET @MailBody =
'<html>' +
'<body>' +
'<p>The Data Archival and Purging Operation is Successfully finished</p>' +
'<p>Here are some of the details for the operation:</p>' +
'<table border="1">' +
'<tr><td>Start Time:</td><td>' + CONVERT(NVARCHAR, @StartTime, 121) + '</td></tr>' +
'<tr><td>End Time:</td><td>' + CONVERT(NVARCHAR, @EndTime, 121) + '</td></tr>' +
'<tr><td>Source Database:</td><td>' + @SourceDatabase + '</td></tr>' +
'<tr><td>Destination Database:</td><td>' + @TargetDatabase + '</td></tr>' +
'<tr><td>Source Table Name:</td><td>' + @SourceTableName + '</td></tr>' +
'<tr><td>Destination Table Name:</td><td>' + @DestinationTableNames + '</td></tr>' +
'<tr><td>Status:</td><td>' + @Status + '</td></tr>' +
'</table>' +
'</body>' +
'</html>';
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQLServer_Data_Purge_Notification',
@recipients = 'abc@gmail.com',
@body = @MailBody,
@subject = 'Completion of Data Archiving and Purging Operation',
@body_format = 'HTML';
END TRY
BEGIN CATCH
SET @EndTime = GETDATE();
DECLARE @primaryKeysList4 NVARCHAR(255);
set @primaryKeysList4 = 'Need to find';
-- Insert a record into Header_Log_Table and get the generated LogsID
SET @SQL = '
USE ' + QUOTENAME(@TargetDatabase) + ';
INSERT INTO ' + QUOTENAME(@TargetSchema) + '.Header_Log_Table (
Purpose,
SourceTableName,
DestinationTableNames,
TablesChanged,
StartTime,
EndTime,
Status
)
VALUES (
@Purpose,
@SourceTableName,
@DestinationTableNames,
@TablesChanged,
@StartTime,
@EndTime,
@Status
);
';
SET @Purpose = @message;
SET @Status = '**** Failed ****';
SET @DestinationTableNames = @TargetDatabase +' . '+ @TargetSchema + ' . ' + @TargetTable;
SET @TablesChanged = 'Deciding';
SET @SourceTableName = @SourceTable;
SET @Message_Req = @message;
EXEC FindChildTablesForParent_Working_5 @parent_table = @SourceTable, @final_list = @final OUTPUT, @changed_list = @change OUTPUT;
EXEC sp_executesql @SQL, N'@Purpose NVARCHAR(MAX), @SourceTableName NVARCHAR(MAX), @DestinationTableNames NVARCHAR(MAX), @TablesChanged NVARCHAR(MAX), @StartTime DATETIME, @EndTime DATETIME, @Status NVARCHAR(50), @LogsID INT OUTPUT',
@Purpose, @SourceTableName, @DestinationTableNames, @change, @StartTime, @EndTime, @Status, @LogsID OUTPUT;
DECLARE @MailBody2 NVARCHAR(MAX);
SET @MailBody2 =
'<html>' +
'<body>' +
'<p>The Data Archival and Purging Operation has Failed</p>' +
'<p>Here are some of the details for the operation:</p>' +
'<table border="1">' +
'<tr><td>Start Time:</td><td>' + CONVERT(NVARCHAR, @StartTime, 121) + '</td></tr>' +
'<tr><td>End Time:</td><td>' + CONVERT(NVARCHAR, @EndTime, 121) + '</td></tr>' +
'<tr><td>Source Database:</td><td>' + @SourceDatabase + '</td></tr>' +
'<tr><td>Destination Database:</td><td>' + @TargetDatabase + '</td></tr>' +
'<tr><td>Source Table Name:</td><td>' + @SourceTableName + '</td></tr>' +
'<tr><td>Destination Table Name:</td><td>' + @DestinationTableNames + '</td></tr>' +
'<tr><td>Status:</td><td>' + @Status + '</td></tr>' +
'</table>' +
'</body>' +
'</html>';
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SQLServer_Data_Purge_Notification',
@recipients = 'abc@gmail.com',
@body = @MailBody2,
@subject = 'Data Archiving and Purging Operation Failed',
@body_format = 'HTML';
END CATCH;
SET @SQL = '
SET IDENTITY_INSERT ' + QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.Child_Log_Table ON;
INSERT INTO ' + QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.Child_Log_Table (LogID, Child_LogsID,TableName, ColumnName, DataType, PrimaryKeysMoved, No_of_Keys_Moved, Columns_Changed)
SELECT logid, childlogid, tablename, column_name, datatype, prim_keys, no_of_prim, columns_changed FROM #tempPrimary;
SET IDENTITY_INSERT ' + QUOTENAME(@TargetDatabase) + '.' + QUOTENAME(@TargetSchema) + '.Child_Log_Table OFF;
';
-- Execute the dynamic SQL
EXEC sp_executesql @SQL;
select * from #tempPrimary;
DROP TABLE #tempPrimary;
END;
drop database TestDB_Backup;
Create database TestDB_Backup;
exec UPDATION_OF_DATA_PROPER_LOGS_FAULT_TOLERANCE_HEADER_CHILD_25 'TestDB', 'dbo', 'ParentTable', 'TestDB_Backup', 'dbo', 'ParentTable', '', '','Just a sample try Again';
select * from TestDB_Backup.dbo.Child_Log_Table;
select * from TestDB_Backup.dbo.Header_Log_Table;
select * from TestDB_Backup.dbo.fix_table_2;
use TestDB;