-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge.sql
600 lines (552 loc) · 22.2 KB
/
merge.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
-------------------------------------------------------------------------------
-- MERGE UTILITIES
-------------------------------------------------------------------------------
-- Copyright (c) 2005-2014 Dave Hughes <[email protected]>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to
-- deal in the Software without restriction, including without limitation the
-- rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
-- sell copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
-- IN THE SOFTWARE.
-------------------------------------------------------------------------------
-- MERGE is an extremely useful command in SQL. Unfortunately, its syntax is
-- excessively verbose (admittedly like much of SQL). A common use-case for
-- MERGE, at least for us, is to update a table from an equivalently structured
-- source. This module contains utility routines which automatically construct
-- the MERGE statements required to do this from information in the system
-- catalog tables. Additional routines are included for automatically
-- generating INSERT and DELETE statements commonly used in similar scenarios.
-------------------------------------------------------------------------------
-- ROLES
-------------------------------------------------------------------------------
-- The following roles grant usage and administrative rights to the objects
-- created by this module.
-------------------------------------------------------------------------------
CREATE ROLE UTILS_MERGE_USER!
CREATE ROLE UTILS_MERGE_ADMIN!
GRANT ROLE UTILS_MERGE_USER TO ROLE UTILS_USER!
GRANT ROLE UTILS_MERGE_USER TO ROLE UTILS_MERGE_ADMIN WITH ADMIN OPTION!
GRANT ROLE UTILS_MERGE_ADMIN TO ROLE UTILS_ADMIN WITH ADMIN OPTION!
-- SQLSTATES
-------------------------------------------------------------------------------
-- The following variables define the set of SQLSTATEs raised by the procedures
-- and functions in this module.
-------------------------------------------------------------------------------
CREATE VARIABLE MERGE_NO_KEY_STATE CHAR(5) CONSTANT '90010'!
CREATE VARIABLE MERGE_PARTIAL_KEY_STATE CHAR(5) CONSTANT '90011'!
CREATE VARIABLE MERGE_SAME_TABLE_STATE CHAR(5) CONSTANT '90012'!
GRANT READ ON VARIABLE MERGE_NO_KEY_STATE TO ROLE UTILS_MERGE_USER!
GRANT READ ON VARIABLE MERGE_NO_KEY_STATE TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
GRANT READ ON VARIABLE MERGE_PARTIAL_KEY_STATE TO ROLE UTILS_MERGE_USER!
GRANT READ ON VARIABLE MERGE_PARTIAL_KEY_STATE TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
GRANT READ ON VARIABLE MERGE_SAME_TABLE_STATE TO ROLE UTILS_MERGE_USER!
GRANT READ ON VARIABLE MERGE_SAME_TABLE_STATE TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
COMMENT ON VARIABLE MERGE_NO_KEY_STATE
IS 'The SQLSTATE raised when an attempt is made to AUTO_MERGE to a target without a unique constraint'!
COMMENT ON VARIABLE MERGE_PARTIAL_KEY_STATE
IS 'The SQLSTATE raised when AUTO_MERGE is run on a key which does not completely exist in the source and target tables'!
COMMENT ON VARIABLE MERGE_SAME_TABLE_STATE
IS 'The SQLSTATE raised when AUTO_MERGE is run with the same table as source and target'!
-- X_BUILD_INSERT(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE)
-- X_BUILD_MERGE(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, DEST_KEY)
-- X_BUILD_DELETE(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, DEST_KEY)
-- X_MERGE_CHECKS(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, DEST_KEY)
-------------------------------------------------------------------------------
-- These functions are effectively private utility subroutines for the
-- procedures defined below. They simply generate snippets of SQL given a set
-- of input parameters.
-------------------------------------------------------------------------------
CREATE FUNCTION X_BUILD_INSERT(
SOURCE_SCHEMA VARCHAR(128),
SOURCE_TABLE VARCHAR(128),
DEST_SCHEMA VARCHAR(128),
DEST_TABLE VARCHAR(128)
)
RETURNS CLOB(64K)
SPECIFIC X_BUILD_INSERT
LANGUAGE SQL
NOT DETERMINISTIC
NO EXTERNAL ACTION
READS SQL DATA
BEGIN ATOMIC
DECLARE COLS CLOB(64K) DEFAULT '';
FOR D AS
SELECT
T.COLNAME AS NAME
FROM
SYSCAT.COLUMNS S
INNER JOIN SYSCAT.COLUMNS T
ON S.COLNAME = T.COLNAME
WHERE
S.TABSCHEMA = SOURCE_SCHEMA
AND S.TABNAME = SOURCE_TABLE
AND T.TABSCHEMA = DEST_SCHEMA
AND T.TABNAME = DEST_TABLE
DO
IF COLS <> '' THEN
SET COLS = COLS || ',';
END IF;
SET COLS = COLS || QUOTE_IDENTIFIER(NAME);
END FOR;
RETURN
'INSERT INTO ' || QUOTE_IDENTIFIER(DEST_SCHEMA) || '.' || QUOTE_IDENTIFIER(DEST_TABLE) || ' '
|| '(' || COLS || ') '
|| 'SELECT ' || COLS || ' '
|| 'FROM ' || QUOTE_IDENTIFIER(SOURCE_SCHEMA) || '.' || QUOTE_IDENTIFIER(SOURCE_TABLE);
END!
CREATE FUNCTION X_BUILD_MERGE(
SOURCE_SCHEMA VARCHAR(128),
SOURCE_TABLE VARCHAR(128),
DEST_SCHEMA VARCHAR(128),
DEST_TABLE VARCHAR(128),
DEST_KEY VARCHAR(128)
)
RETURNS CLOB(64K)
SPECIFIC X_BUILD_MERGE
LANGUAGE SQL
NOT DETERMINISTIC
NO EXTERNAL ACTION
READS SQL DATA
BEGIN ATOMIC
DECLARE JOIN_CLAUSE CLOB(64K) DEFAULT '';
DECLARE INSERT_COLS CLOB(64K) DEFAULT '';
DECLARE INSERT_VALS CLOB(64K) DEFAULT '';
DECLARE UPDATE_COLS CLOB(64K) DEFAULT '';
DECLARE UPDATE_VALS CLOB(64K) DEFAULT '';
FOR D AS
SELECT
T.COLNAME AS NAME,
CASE WHEN K.COLNAME IS NULL
THEN 'N'
ELSE 'Y'
END AS KEY_COL
FROM
SYSCAT.COLUMNS S
INNER JOIN SYSCAT.COLUMNS T
ON S.COLNAME = T.COLNAME
INNER JOIN SYSCAT.TABCONST C
ON T.TABSCHEMA = C.TABSCHEMA
AND T.TABNAME = C.TABNAME
LEFT JOIN SYSCAT.KEYCOLUSE K
ON C.TABSCHEMA = K.TABSCHEMA
AND C.TABNAME = K.TABNAME
AND C.CONSTNAME = K.CONSTNAME
AND T.COLNAME = K.COLNAME
WHERE
S.TABSCHEMA = SOURCE_SCHEMA
AND S.TABNAME = SOURCE_TABLE
AND T.TABSCHEMA = DEST_SCHEMA
AND T.TABNAME = DEST_TABLE
AND C.CONSTNAME = DEST_KEY
AND C.TYPE IN ('P', 'U')
DO
IF D.KEY_COL = 'Y' THEN
IF JOIN_CLAUSE <> '' THEN
SET JOIN_CLAUSE = JOIN_CLAUSE || ' AND ';
END IF;
SET JOIN_CLAUSE = JOIN_CLAUSE ||
'S.' || QUOTE_IDENTIFIER(NAME) || ' = ' ||
'T.' || QUOTE_IDENTIFIER(NAME);
ELSE
IF UPDATE_COLS <> '' THEN
SET UPDATE_COLS = UPDATE_COLS || ',';
SET UPDATE_VALS = UPDATE_VALS || ',';
END IF;
SET UPDATE_COLS = UPDATE_COLS || QUOTE_IDENTIFIER(NAME);
SET UPDATE_VALS = UPDATE_VALS || 'S.' || QUOTE_IDENTIFIER(NAME);
END IF;
IF INSERT_COLS <> '' THEN
SET INSERT_COLS = INSERT_COLS || ',';
SET INSERT_VALS = INSERT_VALS || ',';
END IF;
SET INSERT_COLS = INSERT_COLS || QUOTE_IDENTIFIER(NAME);
SET INSERT_VALS = INSERT_VALS || 'S.' || QUOTE_IDENTIFIER(NAME);
END FOR;
RETURN
'MERGE INTO ' || QUOTE_IDENTIFIER(DEST_SCHEMA) || '.' || QUOTE_IDENTIFIER(DEST_TABLE) || ' AS T '
|| 'USING ' || QUOTE_IDENTIFIER(SOURCE_SCHEMA) || '.' || QUOTE_IDENTIFIER(SOURCE_TABLE) || ' AS S '
|| 'ON ' || JOIN_CLAUSE || ' '
|| 'WHEN MATCHED THEN UPDATE SET (' || UPDATE_COLS || ') = (' || UPDATE_VALS || ') '
|| 'WHEN NOT MATCHED THEN INSERT (' || INSERT_COLS || ') VALUES (' || INSERT_VALS || ')';
END!
CREATE FUNCTION X_BUILD_DELETE(
SOURCE_SCHEMA VARCHAR(128),
SOURCE_TABLE VARCHAR(128),
DEST_SCHEMA VARCHAR(128),
DEST_TABLE VARCHAR(128),
DEST_KEY VARCHAR(128)
)
RETURNS CLOB(64K)
SPECIFIC X_BUILD_DELETE
LANGUAGE SQL
NOT DETERMINISTIC
NO EXTERNAL ACTION
READS SQL DATA
BEGIN ATOMIC
DECLARE KEY_COLS CLOB(64K) DEFAULT '';
FOR D AS
SELECT
T.COLNAME AS NAME,
CASE WHEN K.COLNAME IS NULL
THEN 'N'
ELSE 'Y'
END AS KEY_COL
FROM
SYSCAT.COLUMNS S
INNER JOIN SYSCAT.COLUMNS T
ON S.COLNAME = T.COLNAME
INNER JOIN SYSCAT.TABCONST C
ON T.TABSCHEMA = C.TABSCHEMA
AND T.TABNAME = C.TABNAME
INNER JOIN SYSCAT.KEYCOLUSE K
ON C.TABSCHEMA = K.TABSCHEMA
AND C.TABNAME = K.TABNAME
AND C.CONSTNAME = K.CONSTNAME
AND T.COLNAME = K.COLNAME
WHERE
S.TABSCHEMA = SOURCE_SCHEMA
AND S.TABNAME = SOURCE_TABLE
AND T.TABSCHEMA = DEST_SCHEMA
AND T.TABNAME = DEST_TABLE
AND C.CONSTNAME = DEST_KEY
AND C.TYPE IN ('P', 'U')
DO
IF KEY_COLS <> '' THEN
SET KEY_COLS = KEY_COLS || ',';
END IF;
SET KEY_COLS = KEY_COLS || QUOTE_IDENTIFIER(NAME);
END FOR;
RETURN
'DELETE FROM ' || QUOTE_IDENTIFIER(DEST_SCHEMA) || '.' || QUOTE_IDENTIFIER(DEST_TABLE) || ' '
|| 'WHERE (' || KEY_COLS || ') IN ('
|| 'SELECT ' || KEY_COLS || ' '
|| 'FROM ' || QUOTE_IDENTIFIER(DEST_SCHEMA) || '.' || QUOTE_IDENTIFIER(DEST_TABLE) || ' '
|| 'EXCEPT '
|| 'SELECT ' || KEY_COLS || ' '
|| 'FROM ' || QUOTE_IDENTIFIER(SOURCE_SCHEMA) || '.' || QUOTE_IDENTIFIER(SOURCE_TABLE)
|| ')';
END!
CREATE PROCEDURE X_INSERT_CHECKS(
SOURCE_SCHEMA VARCHAR(128),
SOURCE_TABLE VARCHAR(128),
DEST_SCHEMA VARCHAR(128),
DEST_TABLE VARCHAR(128)
)
SPECIFIC X_INSERT_CHECKS
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL ASSERT_TABLE_EXISTS(SOURCE_SCHEMA, SOURCE_TABLE);
CALL ASSERT_TABLE_EXISTS(DEST_SCHEMA, DEST_TABLE);
-- Check source and target are distinct
IF SOURCE_SCHEMA = DEST_SCHEMA THEN
IF SOURCE_TABLE = DEST_TABLE THEN
CALL SIGNAL_STATE(MERGE_SAME_TABLE_STATE,
'Source and destination tables cannot be the same');
END IF;
END IF;
END!
CREATE PROCEDURE X_MERGE_CHECKS(
SOURCE_SCHEMA VARCHAR(128),
SOURCE_TABLE VARCHAR(128),
DEST_SCHEMA VARCHAR(128),
DEST_TABLE VARCHAR(128),
DEST_KEY VARCHAR(128)
)
SPECIFIC X_MERGE_CHECKS
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL X_INSERT_CHECKS(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE);
-- Check all columns of the destination key are present in the source table
IF (
SELECT COUNT(*)
FROM SYSCAT.KEYCOLUSE
WHERE
TABSCHEMA = DEST_SCHEMA
AND TABNAME = DEST_TABLE
AND CONSTNAME = DEST_KEY
) <> (
SELECT COUNT(*)
FROM
SYSCAT.KEYCOLUSE K
INNER JOIN SYSCAT.COLUMNS C
ON K.COLNAME = C.COLNAME
WHERE
K.TABSCHEMA = DEST_SCHEMA
AND K.TABNAME = DEST_TABLE
AND K.CONSTNAME = DEST_KEY
AND C.TABSCHEMA = SOURCE_SCHEMA
AND C.TABNAME = SOURCE_TABLE
) THEN
CALL SIGNAL_STATE(MERGE_PARTIAL_KEY_STATE,
'All fields of constraint ' || DEST_KEY ||
' must exist in the source and the target tables');
END IF;
END!
-- AUTO_INSERT(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE)
-- AUTO_INSERT(SOURCE_TABLE, DEST_TABLE)
-------------------------------------------------------------------------------
-- The AUTO_INSERT procedure inserts all data from SOURCE_TABLE into DEST_TABLE
-- by means of an automatically generated INSERT statement covering all columns
-- common to both tables.
--
-- If SOURCE_SCHEMA and DEST_SCHEMA are not specified they default to the
-- current schema.
-------------------------------------------------------------------------------
CREATE PROCEDURE AUTO_INSERT(
SOURCE_SCHEMA VARCHAR(128),
SOURCE_TABLE VARCHAR(128),
DEST_SCHEMA VARCHAR(128),
DEST_TABLE VARCHAR(128)
)
SPECIFIC AUTO_INSERT1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
DECLARE DML CLOB(64K) DEFAULT '';
CALL X_INSERT_CHECKS(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE);
SET DML = X_BUILD_INSERT(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE);
EXECUTE IMMEDIATE DML;
END!
CREATE PROCEDURE AUTO_INSERT(
SOURCE_TABLE VARCHAR(128),
DEST_TABLE VARCHAR(128)
)
SPECIFIC AUTO_INSERT2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL AUTO_INSERT(CURRENT SCHEMA, SOURCE_TABLE, CURRENT SCHEMA, DEST_TABLE);
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_INSERT1 TO ROLE UTILS_MERGE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_INSERT2 TO ROLE UTILS_MERGE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_INSERT1 TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_INSERT2 TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE AUTO_INSERT1
IS 'Automatically inserts data from SOURCE_TABLE into DEST_TABLE'!
COMMENT ON SPECIFIC PROCEDURE AUTO_INSERT2
IS 'Automatically inserts data from SOURCE_TABLE into DEST_TABLE'!
-- AUTO_MERGE(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, DEST_KEY)
-- AUTO_MERGE(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE)
-- AUTO_MERGE(SOURCE_TABLE, DEST_TABLE, DEST_KEY)
-- AUTO_MERGE(SOURCE_TABLE, DEST_TABLE)
-------------------------------------------------------------------------------
-- The AUTO_MERGE procedure performs an "upsert", or combined insert and update
-- of all data from SOURCE_TABLE into DEST_TABLE by means of an automatically
-- generated MERGE statement.
--
-- The DEST_KEY parameter specifies the name of the unique key to use for
-- identifying rows in the destination table. If specified, it must be the name
-- of a unique key or primary key which covers columns which exist in both the
-- source and destination tables. If omitted, it defaults to the name of the
-- primary key of the destination table.
--
-- If SOURCE_SCHEMA and DEST_SCHEMA are not specified they default to the
-- current schema.
-------------------------------------------------------------------------------
CREATE PROCEDURE AUTO_MERGE(
SOURCE_SCHEMA VARCHAR(128),
SOURCE_TABLE VARCHAR(128),
DEST_SCHEMA VARCHAR(128),
DEST_TABLE VARCHAR(128),
DEST_KEY VARCHAR(128)
)
SPECIFIC AUTO_MERGE1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
DECLARE DML CLOB(64K) DEFAULT '';
CALL X_MERGE_CHECKS(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, DEST_KEY);
SET DML = X_BUILD_MERGE(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, DEST_KEY);
EXECUTE IMMEDIATE DML;
END!
CREATE PROCEDURE AUTO_MERGE(
SOURCE_SCHEMA VARCHAR(128),
SOURCE_TABLE VARCHAR(128),
DEST_SCHEMA VARCHAR(128),
DEST_TABLE VARCHAR(128)
)
SPECIFIC AUTO_MERGE2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL AUTO_MERGE(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, (
SELECT CONSTNAME
FROM SYSCAT.TABCONST
WHERE TABSCHEMA = DEST_SCHEMA
AND TABNAME = DEST_TABLE
AND TYPE = 'P'));
END!
CREATE PROCEDURE AUTO_MERGE(
SOURCE_TABLE VARCHAR(128),
DEST_TABLE VARCHAR(128),
DEST_KEY VARCHAR(128)
)
SPECIFIC AUTO_MERGE3
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL AUTO_MERGE(CURRENT SCHEMA, SOURCE_TABLE, CURRENT SCHEMA, DEST_TABLE, DEST_KEY);
END!
CREATE PROCEDURE AUTO_MERGE(
SOURCE_TABLE VARCHAR(128),
DEST_TABLE VARCHAR(128)
)
SPECIFIC AUTO_MERGE4
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL AUTO_MERGE(CURRENT SCHEMA, SOURCE_TABLE, CURRENT SCHEMA, DEST_TABLE, (
SELECT CONSTNAME
FROM SYSCAT.TABCONST
WHERE TABSCHEMA = CURRENT SCHEMA
AND TABNAME = DEST_TABLE
AND TYPE = 'P'));
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_MERGE1 TO ROLE UTILS_MERGE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_MERGE2 TO ROLE UTILS_MERGE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_MERGE3 TO ROLE UTILS_MERGE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_MERGE4 TO ROLE UTILS_MERGE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_MERGE1 TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_MERGE2 TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_MERGE3 TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_MERGE4 TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE AUTO_MERGE1
IS 'Automatically inserts/updates ("upserts") data from SOURCE_TABLE into DEST_TABLE based on DEST_KEY'!
COMMENT ON SPECIFIC PROCEDURE AUTO_MERGE2
IS 'Automatically inserts/updates ("upserts") data from SOURCE_TABLE into DEST_TABLE based on DEST_KEY'!
COMMENT ON SPECIFIC PROCEDURE AUTO_MERGE3
IS 'Automatically inserts/updates ("upserts") data from SOURCE_TABLE into DEST_TABLE based on DEST_KEY'!
COMMENT ON SPECIFIC PROCEDURE AUTO_MERGE4
IS 'Automatically inserts/updates ("upserts") data from SOURCE_TABLE into DEST_TABLE based on DEST_KEY'!
-- AUTO_DELETE(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, DEST_KEY)
-- AUTO_DELETE(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE)
-- AUTO_DELETE(SOURCE_TABLE, DEST_TABLE, DEST_KEY)
-- AUTO_DELETE(SOURCE_TABLE, DEST_TABLE)
-------------------------------------------------------------------------------
-- The AUTO_DELETE procedure deletes rows from DEST_TABLE that do not exist
-- in SOURCE_TABLE. This procedure is intended to be used after the AUTO_MERGE
-- procedure has been used to upsert from SOURCE to DEST.
--
-- The DEST_KEY parameter specifies the name of the unique key to use for
-- identifying rows in the destination table. If specified, it must be the name
-- of a unique key or primary key which covers columns which exist in both the
-- source and destination tables. If omitted, it defaults to the name of the
-- primary key of the destination table.
--
-- If SOURCE_SCHEMA and DEST_SCHEMA are not specified they default to the
-- current schema.
-------------------------------------------------------------------------------
CREATE PROCEDURE AUTO_DELETE(
SOURCE_SCHEMA VARCHAR(128),
SOURCE_TABLE VARCHAR(128),
DEST_SCHEMA VARCHAR(128),
DEST_TABLE VARCHAR(128),
DEST_KEY VARCHAR(128)
)
SPECIFIC AUTO_DELETE1
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
DECLARE DML CLOB(64K) DEFAULT '';
CALL X_MERGE_CHECKS(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, DEST_KEY);
SET DML = X_BUILD_DELETE(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, DEST_KEY);
EXECUTE IMMEDIATE DML;
END!
CREATE PROCEDURE AUTO_DELETE(
SOURCE_SCHEMA VARCHAR(128),
SOURCE_TABLE VARCHAR(128),
DEST_SCHEMA VARCHAR(128),
DEST_TABLE VARCHAR(128)
)
SPECIFIC AUTO_DELETE2
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL AUTO_DELETE(SOURCE_SCHEMA, SOURCE_TABLE, DEST_SCHEMA, DEST_TABLE, (
SELECT CONSTNAME
FROM SYSCAT.TABCONST
WHERE TABSCHEMA = DEST_SCHEMA
AND TABNAME = DEST_TABLE
AND TYPE = 'P'));
END!
CREATE PROCEDURE AUTO_DELETE(
SOURCE_TABLE VARCHAR(128),
DEST_TABLE VARCHAR(128),
DEST_KEY VARCHAR(128)
)
SPECIFIC AUTO_DELETE3
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL AUTO_DELETE(CURRENT SCHEMA, SOURCE_TABLE, CURRENT SCHEMA, DEST_TABLE, DEST_KEY);
END!
CREATE PROCEDURE AUTO_DELETE(
SOURCE_TABLE VARCHAR(128),
DEST_TABLE VARCHAR(128)
)
SPECIFIC AUTO_DELETE4
MODIFIES SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
LANGUAGE SQL
BEGIN ATOMIC
CALL AUTO_DELETE(CURRENT SCHEMA, SOURCE_TABLE, CURRENT SCHEMA, DEST_TABLE, (
SELECT CONSTNAME
FROM SYSCAT.TABCONST
WHERE TABSCHEMA = CURRENT SCHEMA
AND TABNAME = DEST_TABLE
AND TYPE = 'P'));
END!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_DELETE1 TO ROLE UTILS_MERGE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_DELETE2 TO ROLE UTILS_MERGE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_DELETE3 TO ROLE UTILS_MERGE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_DELETE4 TO ROLE UTILS_MERGE_USER!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_DELETE1 TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_DELETE2 TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_DELETE3 TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
GRANT EXECUTE ON SPECIFIC PROCEDURE AUTO_DELETE4 TO ROLE UTILS_MERGE_ADMIN WITH GRANT OPTION!
COMMENT ON SPECIFIC PROCEDURE AUTO_DELETE1
IS 'Automatically removes data from DEST_TABLE that doesn''t exist in SOURCE_TABLE, based on DEST_KEY'!
COMMENT ON SPECIFIC PROCEDURE AUTO_DELETE2
IS 'Automatically removes data from DEST_TABLE that doesn''t exist in SOURCE_TABLE, based on DEST_KEY'!
COMMENT ON SPECIFIC PROCEDURE AUTO_DELETE3
IS 'Automatically removes data from DEST_TABLE that doesn''t exist in SOURCE_TABLE, based on DEST_KEY'!
COMMENT ON SPECIFIC PROCEDURE AUTO_DELETE4
IS 'Automatically removes data from DEST_TABLE that doesn''t exist in SOURCE_TABLE, based on DEST_KEY'!
-- vim: set et sw=4 sts=4: