1- -- Watermark table for SPROC
2- CREATE TABLE watermark_test (table_name VARCHAR (MAX));
3-
4- -- Target table when moving data from staging
5- CREATE TABLE test_table
6- (
7- col1 INTEGER NOT NULL ,
8- col2 INTEGER NOT NULL ,
9- col3 VARCHAR (20 ) NOT NULL ,
10- );
11-
12- CREATE TABLE expected_test_table
13- (
14- col1 INTEGER NOT NULL ,
15- col2 INTEGER NOT NULL ,
16- col3 VARCHAR (20 ) NOT NULL ,
17- );
18-
19- -- Staging table for moving data to target table
20- SELECT TOP 0 * INTO dbo .staging_test_table FROM dbo .test_table ;
21-
22- INSERT INTO staging_test_table VALUES (1 , 2 , ' test' );
23- INSERT INTO expected_test_table VALUES (1 , 2 , ' test' );
24- INSERT INTO watermark_test VALUES (' test_table' );
25-
26- EXEC dbo .move_data_to_permanent_tables ' watermark_test' ;
27-
28-
29- -- Validate test table has the same number of rows as expected table
30- IF (SELECT COUNT (* ) FROM test_table) = (SELECT COUNT (* ) FROM expected_test_table)
31- RAISERROR (' Equal' , 0 , 1 ) WITH NOWAIT
32- ELSE
33- RAISERROR (' Not Equal' , 0 , 1 ) WITH NOWAIT
34-
35- -- Validate test table has the same value for col1 as expected table
36- IF (SELECT col1 FROM test_table) = (SELECT col1 FROM expected_test_table)
37- RAISERROR (' Equal' , 0 , 1 ) WITH NOWAIT
38- ELSE
39- RAISERROR (' Not Equal' , 0 , 1 ) WITH NOWAIT
40-
41-
42- -- Validate test table has the same value for col2 as expected table
43- IF (SELECT col2 FROM test_table) = (SELECT col2 FROM expected_test_table)
44- RAISERROR (' Equal' , 0 , 1 ) WITH NOWAIT
45- ELSE
46- RAISERROR (' Not Equal' , 0 , 1 ) WITH NOWAIT
47-
48-
49- -- Validate test table has the same value for col3 as expected table
50- IF (SELECT col3 FROM test_table) = (SELECT col3 FROM expected_test_table)
51- RAISERROR (' Equal' , 0 , 1 ) WITH NOWAIT
52- ELSE
53- RAISERROR (' Not Equal' , 0 , 1 ) WITH NOWAIT
54-
55- DROP TABLE watermark_test;
56- DROP TABLE test_table;
57- DROP TABLE staging_test_table;
58- DROP TABLE expected_test_table;
1+ CREATE PROCEDURE test_move_data_to_permanent_tables
2+ AS
3+ BEGIN
4+ -- Watermark table for SPROC
5+ CREATE TABLE watermark_test (table_name VARCHAR (MAX));
6+
7+ -- Target table when moving data from staging
8+ CREATE TABLE test_table
9+ (
10+ col1 INTEGER NOT NULL ,
11+ col2 INTEGER NOT NULL ,
12+ col3 VARCHAR (20 ) NOT NULL ,
13+ );
14+
15+ CREATE TABLE expected_test_table
16+ (
17+ col1 INTEGER NOT NULL ,
18+ col2 INTEGER NOT NULL ,
19+ col3 VARCHAR (20 ) NOT NULL ,
20+ );
21+
22+ -- Staging table for moving data to target table
23+ SELECT TOP 0 * INTO dbo .staging_test_table FROM dbo .test_table ;
24+
25+ INSERT INTO staging_test_table VALUES (1 , 2 , ' test' );
26+ INSERT INTO expected_test_table VALUES (1 , 2 , ' test' );
27+ INSERT INTO watermark_test VALUES (' test_table' );
28+
29+ EXEC dbo .move_data_to_permanent_tables ' watermark_test' ;
30+
31+
32+ -- Validate test table has the same number of rows as expected table
33+ IF (SELECT COUNT (* ) FROM test_table) = (SELECT COUNT (* ) FROM expected_test_table)
34+ RAISERROR (' Equal' , 0 , 1 ) WITH NOWAIT
35+ ELSE
36+ RAISERROR (' Not Equal' , 0 , 1 );
37+
38+ -- Validate test table has the same value for col1 as expected table
39+ IF (SELECT col1 FROM test_table) = (SELECT col1 FROM expected_test_table)
40+ RAISERROR (' Equal' , 0 , 1 ) WITH NOWAIT;
41+ ELSE
42+ RAISERROR (' Not Equal' , 0 , 1 );
43+
44+
45+ -- Validate test table has the same value for col2 as expected table
46+ IF (SELECT col2 FROM test_table) = (SELECT col2 FROM expected_test_table)
47+ RAISERROR (' Equal' , 0 , 1 ) WITH NOWAIT
48+ ELSE
49+ RAISERROR (' Not Equal' , 0 , 1 );
50+
51+ -- Validate test table has the same value for col3 as expected table
52+ IF (SELECT col3 FROM test_table) = (SELECT col3 FROM expected_test_table)
53+ RAISERROR (' Equal' , 0 , 1 ) WITH NOWAIT
54+ ELSE
55+ RAISERROR (' Not Equal' , 0 , 1 );
56+
57+ DROP TABLE watermark_test;
58+ DROP TABLE test_table;
59+ DROP TABLE staging_test_table;
60+ DROP TABLE expected_test_table;
61+ END
0 commit comments