-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutodistribute V6.sql
673 lines (602 loc) · 25.1 KB
/
Autodistribute V6.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
CREATE OR ALTER FUNCTION wh.for_x_in (
@array_of_strings VARCHAR(MAX),
@operation VARCHAR(200),
@concat VARCHAR(10) = ','
) RETURNS VARCHAR(MAX) AS
BEGIN
/*
FUNCTION for_x_in
AUTHOR:
-------
R. Wilmans-Sangwienwong (2021)
PURPOSE:
--------
To mimic the common 'for x in ' construct with ARRAYs.
In MS SQL Server no arrays are available, so here mimic this using a comma-separated string of elements.
PARAMETERS:
-----------
array_of_strings : a string of comma-separated elements
Datatype: VARCHAR(MAX)
Default: -
operation : The string containing the operation where '$' is replaced by the element-at-hand (element from the 'array')
Datatype: VARCHAR(200)
Default: -
concat : the separator between the modified elements.
Datatype: VARCHAR(10)
Default: ','
RETURNS:
--------
A concatenated string of modified elements from a string-of-elements.
REMARKS:
--------
In the operation, use '$' (without quotes) as a placeholder for the element
Example:
-- ---------------------------------------------------------------
for_x_in('date_column_1,datetimestamp_2','ins.$ < cur.$','AND')
SELECT for_x_in('eerste,tweede','in1.$ = out1.$',DEFAULT);
SELECT for_x_in('eerste,tweede','in1.$ = out1.$','AND');
-- ---------------------------------------------------------------
HISTORY:
--------
2022-08-15 RWS Adjusted for Brinks
*/
DECLARE @result VARCHAR(MAX) = '';
SELECT @result = @result + CASE WHEN @result = '' THEN '' ELSE ' ' + @concat + ' ' END + REPLACE(@operation,'$',value)
FROM STRING_SPLIT(@array_of_strings,',')
;
RETURN @result;
END
;
-- Function to mimic the distribution of records on temporal tables
CREATE OR ALTER PROCEDURE wh.stp_autodistribute (
@trigger_id INTEGER = 0,
@insert_table_name VARCHAR(128) = '#inserted',
@debug CHAR(1) = 'N' -- Y/N
) AS
BEGIN
/*
PROCEDURE stp_autodistribute
AUTHOR:
-------
R. Wilmans-Sangwienwong (2021)
PURPOSE:
--------
Procedure to facilitate era-partitioned tables. Past, present (and future not implemented for now).
To insert records and keep historical records, without copying and updating or inserting records actively - this is all done
behind the scenes.
In MS SQL Server, table inheritance like in PostgreSQL is not implemented. So here, instead of a base table, make use of a VIEW
('<table>') with a ON INSERT DO INSTEAD. The current situation is stored in <table>__curr, the historical records in <table>__hist.
If needed, the current state can be obtained from <table>__curr - this will be faster than going through the entire set including
historical records.
PARAMETERS:
-----------
trigger_id : the object-id (OID) of the calling trigger. This is used to determine which table (+__curr/__hist) is being inserted into. REQUIRED
Datatype: INTEGER
Default: 0 (=no object)
insert_table_name : The name of the TEMP table that is a 1:1 copy of the 'magic' INSERTED table. This is necessary, as Dynamic SQL is being used,
and the magic table is not reacheable by Dynamic SQL (because of context).
Datatype: VARCHAR(128)
Default: '#inserted'
debug : If extra (debug!) information is needed (like the actual queries that are being performed), then set debug to 'Y'.
Datatype: CHAR(1)
Default: 'N'
RETURNS:
--------
Nothing, but updates the <table>__curr and <table>__hist (if necessary), which is reflected in the <table>-VIEW.
REMARKS:
--------
<table>__futr NOT YET implemented.
Function wh.for_x_in REQUIRED!
HISTORY:
--------
YYYY-MM-DD Who Change
2022-02-02 RWS Separated the insertion into <table>__curr and the updating of the corresponding SEQUENCES.
If no sequences were available (as is the case in an intermediate/linking table) the insert statement
would not run either.
Example:
-- ---------------------------------------------------------------
DROP VIEW IF EXISTS wh.double_pk;
DROP TABLE IF EXISTS wh.double_pk__hist;
DROP TABLE IF EXISTS wh.double_pk__curr;
DROP SEQUENCE IF EXISTS wh.seq_double_pk__double_pk_1_id;
DROP SEQUENCE IF EXISTS wh.seq_double_pk__double_pk_2_id;
-- Sequence [meta].[seq_rule__rule_id]
CREATE SEQUENCE wh.seq_double_pk__double_pk_1_id AS int START WITH 1 INCREMENT BY 1 CACHE;
CREATE SEQUENCE wh.seq_double_pk__double_pk_2_id AS int START WITH 1 INCREMENT BY 1 CACHE;
-- The table representing the current (or most recent) situation
CREATE TABLE wh.double_pk__curr (
double_pk_1_id INT NOT NULL DEFAULT NEXT VALUE FOR wh.seq_double_pk__double_pk_1_id,
double_pk_2_id INT NOT NULL DEFAULT NEXT VALUE FOR wh.seq_double_pk__double_pk_2_id,
meta_valid_from DATE NOT NULL DEFAULT GETDATE(),
meta_insert_ts DATETIME2(3) NOT NULL DEFAULT GETDATE(),
meta_is_valid BIT NOT NULL DEFAULT 'TRUE',
meta_is_historical AS CAST(0 AS BIT),
rest_of_vars VARCHAR(20) NOT NULL DEFAULT '---',
CONSTRAINT pk_double_pk_curr PRIMARY KEY CLUSTERED (double_pk_1_id, double_pk_2_id)
);
-- The table with historical data (or past states)
CREATE TABLE wh.double_pk__hist (
double_pk_1_id INT NOT NULL,
double_pk_2_id INT NOT NULL,
meta_valid_from DATE NOT NULL DEFAULT GETDATE(),
meta_insert_ts DATETIME2(3) NOT NULL DEFAULT GETDATE(),
meta_is_valid BIT NOT NULL DEFAULT 'TRUE',
meta_is_historical AS CAST(1 AS BIT),
rest_of_vars VARCHAR(20) NOT NULL DEFAULT '---',
CONSTRAINT pk_double_pk_hist PRIMARY KEY CLUSTERED (meta_valid_from, double_pk_1_id, double_pk_2_id),
CONSTRAINT fk_double_pk_hist__double_pk_curr FOREIGN KEY (double_pk_1_id, double_pk_2_id) REFERENCES wh.double_pk__curr (double_pk_1_id, double_pk_2_id) ON UPDATE CASCADE ON DELETE CASCADE-- NOT FOR REPLICATION
);
GO
;
-- View combining both the current and historical states
CREATE OR ALTER VIEW wh.double_pk AS
SELECT double_pk_1_id,
double_pk_2_id,
meta_valid_from,
meta_insert_ts,
meta_is_valid,
meta_is_historical,
rest_of_vars
FROM wh.double_pk__curr
UNION
SELECT double_pk_1_id,
double_pk_2_id,
meta_valid_from,
meta_insert_ts,
meta_is_valid,
meta_is_historical,
rest_of_vars
FROM wh.double_pk__hist
;
-- The actual INSTEAD - trigger on the VIEW
CREATE OR ALTER TRIGGER wh.trg_double_pk__ins
ON wh.double_pk
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
-- ------------------------------------
-- Get everything from 'magic' table into TEMP table, so we can access it in the procedure and Dynamic SQL...
SELECT *
INTO #inserted_double_pk
FROM INSERTED;
;
EXECUTE wh.stp_autodistribute @trigger_id = @@PROCID, @insert_table_name = '#inserted_double_pk';
END
;
GO
;
-- --------------------------------
-- First record - new ID assigned
INSERT INTO wh.double_pk (meta_valid_from, rest_of_vars)
VALUES ('20200101', 'AA')
;
-- --------------------------------
-- Additional records - new ID
INSERT INTO wh.double_pk (meta_valid_from, rest_of_vars)
VALUES ('20200101', 'ZZ')
;
-- --------------------------------
-- New current state; copy to hist + update
INSERT INTO wh.double_pk (double_pk_1_id, double_pk_2_id, meta_valid_from, rest_of_vars)
VALUES (1,1,'20200222', 'BB')
;
-- --------------------------------
-- New current state; copy to hist + update
INSERT INTO wh.double_pk (double_pk_1_id, double_pk_2_id, meta_valid_from, rest_of_vars)
VALUES (1,1,'20200313', 'CC')
;
-- --------------------------------
-- Update historical record
INSERT INTO wh.double_pk (double_pk_1_id, double_pk_2_id, meta_valid_from, rest_of_vars)
VALUES (1,1,'20200101', 'DD')
;
-- --------------------------------
-- Update current record
INSERT INTO wh.double_pk (double_pk_1_id, double_pk_2_id, meta_valid_from, rest_of_vars)
VALUES (1,1,'20200313', 'QQ')
;
-- --------------------------------
-- List from-to for certain record
SELECT *
FROM wh.double_pk
;
SELECT double_pk_1_id, double_pk_2_id,
meta_valid_from,
LEAD(meta_valid_from,1,'9999/01/01') OVER (PARTITION BY double_pk_1_id, double_pk_2_id ORDER BY meta_valid_from) AS meta_valid_to,
d.*
FROM wh.double_pk AS d
WHERE double_pk_1_id =1 AND double_pk_2_id = 1
;
-- --------------------------------
-- next-value to-be-generated by sequence 1 higher than MAX(id)?
INSERT INTO wh.double_pk (double_pk_1_id, double_pk_2_id, meta_valid_from, rest_of_vars)
VALUES (5,1,'20200313', 'YY')
;
SELECT CAST(CURRENT_VALUE AS INT) AS next_value
FROM SYS.sequences
WHERE name = 'seq_double_pk__double_pk_1_id'
;
-- --------------------------------------------------------------
*/
SET NOCOUNT ON;
DECLARE @pk_column VARCHAR(1200) = '';
DECLARE @pk_column_type VARCHAR(1200) = '';
DECLARE @pk_column_default VARCHAR(1200) = '';
DECLARE @seq_id_arr VARCHAR(1400) = '';;
DECLARE @sec_pk_col VARCHAR(128) = '';
DECLARE @sec_pk_col_type VARCHAR(128) = '';
DECLARE @sec_pk_col_default VARCHAR(200) = '';
DECLARE @meta_ts VARCHAR(128) = '';
DECLARE @meta_ts_type VARCHAR(128) = '';
DECLARE @meta_ts_default VARCHAR(200) = '';
-- 8K should be enough... (so as not to use 'MAX') - this can be changed, of course...
DECLARE @non_pk_cols VARCHAR(8000) = '';
DECLARE @non_pk_cols_defaults VARCHAR(8000) = '';
DECLARE @table_curr_id INT;
DECLARE @table_hist_id INT;
DECLARE @table_curr VARCHAR(257); -- 128 + '.' + 128
DECLARE @table_hist VARCHAR(257);
DECLARE @sql NVARCHAR(MAX) = '';
-- -----------------------------------------------------------------------------
-- Retrieve the basics....
SELECT @pk_column = @pk_column + IIF(@pk_column = '','',',') + LTRIM(col.name),
@pk_column_default = @pk_column_default + IIF(@pk_column_default = '','',',') + col.name + ' = COALESCE(' + col.name + ',' + e.dflt + ')' ,
@pk_column_type = @pk_column_type + IIF(@pk_column_type = '','',',') + col.name + ' ' + e.datatype,
@seq_id_arr = @seq_id_arr + IIF(@seq_id_arr = '','',',') + LTRIM(OBJECT_ID(SUBSTRING(e.dflt, e2.pat_pos, LEN(e.dflt) - CHARINDEX(']',REVERSE(e.dflt)) - e2.pat_pos+2))) + '#' + LTRIM(col.name),
@table_curr_id = t.table_curr_id,
@table_hist_id = t.table_hist_id,
@table_curr = t.table_curr,
@table_hist = t.table_hist
FROM ( SELECT OBJECT_ID(e.table_curr) AS table_curr_id,
OBJECT_ID(e.table_hist) AS table_hist_id,
e.table_curr,
e.table_hist
FROM SYS.triggers
CROSS APPLY (SELECT OBJECT_SCHEMA_NAME(parent_id) + '.' + OBJECT_NAME(parent_id) + '__curr' AS table_curr,
OBJECT_SCHEMA_NAME(parent_id) + '.' + OBJECT_NAME(parent_id) + '__hist' AS table_hist
) AS e
WHERE object_id = @trigger_id
) AS t
INNER JOIN
SYS.tables AS b
ON b.object_id = t.table_curr_id
INNER JOIN
SYS.key_constraints AS kc
ON kc.parent_object_id = b.object_id AND
kc.type = 'PK'
INNER JOIN
SYS.index_columns AS ic
ON kc.parent_object_id = ic.object_id AND
kc.unique_index_id = ic.index_id
INNER JOIN
SYS.columns AS col
ON ic.object_id = col.object_id AND
ic.column_id = col.column_id
CROSS APPLY ( SELECT CASE
WHEN UPPER(tp.name) LIKE '%CHAR%' THEN UPPER(tp.name) + '('+ LTRIM(col.max_length) + ')'
WHEN UPPER(tp.name) LIKE '%TIME%' AND col.scale != 0 THEN UPPER(tp.name) + '('+ LTRIM(col.scale) + ')'
ELSE UPPER(tp.name)
END AS datatype,
OBJECT_DEFINITION(col.default_object_id) AS dflt
FROM SYS.types AS tp
WHERE col.system_type_id = tp.system_type_id
) AS e
CROSS APPLY ( SELECT PATINDEX('%[[]%].[[]%]%', e.dflt) AS pat_pos
) AS e2
;
IF (@debug = 'Y') BEGIN
PRINT 'pk_column: ' + @pk_column;
PRINT 'pk_column_default: ' + @pk_column_default;
PRINT 'pk_column_type: ' + @pk_column_type;
PRINT 'seq_id_arr: ' + @seq_id_arr;
PRINT 'table_curr_id: ' + LTRIM(@table_curr_id);
PRINT 'table_hist_id: ' + LTRIM(@table_hist_id);
END;
-- -----------------------------------------------------------------------------
-- Secondary PK column
-- So, the column that is part of the PK in __hist, but NOT part of PK in __curr
-- Therefore: 'meta_valid_from' (or whatever that's called)
SELECT @sec_pk_col = col.name,
@sec_pk_col_default = col.name + ' = COALESCE(' + col.name + ', ' + COALESCE(OBJECT_DEFINITION(tid.default_object_id),'CAST(GETDATE() AS VARCHAR)') + ')',
@sec_pk_col_type = col.name + ' ' + e.datatype
FROM SYS.key_constraints AS kc
INNER JOIN
SYS.index_columns AS ic
ON kc.parent_object_id = ic.object_id AND
kc.unique_index_id = ic.index_id
INNER JOIN
SYS.columns AS col
ON ic.object_id = col.object_id AND
ic.column_id = col.column_id
INNER JOIN
SYS.columns AS tid -- From the __curr-table; to make sure that this column is available in BOTH tables!
ON tid.object_id = @table_curr_id AND
tid.name = col.name AND
--col.name NOT IN (@pk_column) -- This doesn't work in SQL Server.... *-SIGH-*
',' + @pk_column + ',' NOT LIKE '%,' + col.name + ',%' -- ... but NOT a defined as PK in __curr!
CROSS APPLY ( SELECT CASE
WHEN UPPER(tp.name) LIKE '%CHAR%' THEN UPPER(tp.name) + '('+ LTRIM(tid.max_length) + ')'
WHEN UPPER(tp.name) LIKE '%TIME%' AND col.scale != 0 THEN UPPER(tp.name) + '('+ LTRIM(tid.scale) + ')'
ELSE UPPER(tp.name) -- Probably just a regular INT (or BIGINT)
END AS datatype
FROM SYS.types AS tp
WHERE tid.system_type_id = tp.system_type_id
) AS e
WHERE kc.parent_object_id = @table_hist_id AND
kc.type = 'PK' -- Primary Key
;
IF (@debug = 'Y') BEGIN
PRINT 'sec_pk_col: ' + @sec_pk_col;
PRINT 'sec_pk_col_default: ' + @sec_pk_col_default;
PRINT 'sec_pk_col_type: ' + @sec_pk_col_type;
END;
-- -----------------------------------------------------------------------------
-- Meta info: timestamp for insertion (/update)
SELECT @meta_ts = col.name,
@meta_ts_default = col.name + ' = COALESCE(' + col.name + ', ' + COALESCE(OBJECT_DEFINITION(col.default_object_id),'CAST(GETDATE() AS VARCHAR)') + ')',
@meta_ts_type = col.name + ' ' + e.datatype
FROM SYS.columns as col
INNER JOIN
SYS.types AS tp
ON col.system_type_id = tp.system_type_id AND
UPPER(tp.name) LIKE 'DATETIME%'
CROSS APPLY ( SELECT CASE
WHEN UPPER(tp.name) LIKE '%CHAR%' THEN UPPER(tp.name) + '('+ LTRIM(col.max_length) + ')'
WHEN UPPER(tp.name) LIKE '%TIME%' AND col.scale != 0 THEN UPPER(tp.name) + '('+ LTRIM(col.scale) + ')'
ELSE UPPER(tp.name)
END AS datatype
FROM SYS.types AS tp
WHERE col.system_type_id = tp.system_type_id
) AS e
WHERE col.object_id = @table_curr_id AND
',' + @pk_column + ',' + @sec_pk_col + ',' NOT LIKE '%,' + col.name + ',%' AND --So, NONE of the PK-columns or Secondary PK-column
UPPER(col.name) LIKE 'META_%'
;
-- -----------------------------------------------------------------------------
-- Rest of columns
SELECT @non_pk_cols = @non_pk_cols + IIF(@non_pk_cols = '','',',') + name,
@non_pk_cols_defaults = @non_pk_cols_defaults + IIF(default_object_id = 0,'', IIF(@non_pk_cols_defaults = '','',',') + name + ' = COALESCE(' + name + ',' + OBJECT_DEFINITION(default_object_id) + ')')
FROM SYS.columns
WHERE object_id = @table_curr_id AND
',' + @pk_column + ',' + @sec_pk_col + ',' + @meta_ts + ',' NOT LIKE '%,' + name + ',%' AND --So, NONE of so-far found special columns
is_computed = 0
ORDER BY column_id
;
IF (@debug = 'Y') BEGIN
PRINT 'non_pk_cols: ' + @non_pk_cols;
END;
/* ************************************************************************************************ */
/* Check if necessary columns are given (aside from key columns, for which we will insert defaults) */
/* ************************************************************************************************ */
-- Update required columns if needed
-- However, not the PK, as this will be the automagical NEXT VALUE FOR sequence...
SET @sql = N' UPDATE ins
SET ' + @sec_pk_col_default + ',
' + @meta_ts_default + ',
' + @non_pk_cols_defaults + '
FROM ' + @insert_table_name + ' AS ins
';
IF (@debug = 'Y') BEGIN
PRINT @sql;
END;
EXECUTE(@sql);
-- -----------------------------------------------------------------------------
-- Already exists and new record is newer (or as new, but with different values) than 'current'
-- So, copy current state to __hist
-- Create TEMP table to store affected IDs
-- Yes, yes, SQL injection... however, the variables are generated from... SYS, so , SQL injections will be VERY DIFFICULT!
-- (by entering code for table / column names - which will be prevented by the DB)
-- NEW RECORD, safe history
SET @sql = N' CREATE TABLE #insert_ids (' +@pk_column_type + ');
INSERT INTO ' + @table_hist + ' (' + @pk_column + ', ' + @sec_pk_col + ', ' + @meta_ts + ', ' + @non_pk_cols + ')
OUTPUT ' + wh.for_x_in (@pk_column, 'INSERTED.$',DEFAULT) + '
INTO #insert_ids (' + @pk_column + ')
SELECT ' +
wh.for_x_in (@pk_column, 'incur.$',DEFAULT) + ', '+
wh.for_x_in (@sec_pk_col, 'incur.$',DEFAULT) + ', '+
wh.for_x_in (@meta_ts, 'incur.$',DEFAULT) + ', '+
wh.for_x_in (@non_pk_cols, 'incur.$',DEFAULT) + '
FROM ' + @insert_table_name + ' AS ins
INNER JOIN
' + @table_curr + ' AS incur
ON ' +
-- ..._id - so, id MUST already exist!
wh.for_x_in(@pk_column,'ins.$ = incur.$','AND') +
' AND ' +
wh.for_x_in(@sec_pk_col,'ins.$ > incur.$','AND') + ' -- meta_valid_from AFTER current state, so NEW current state!
WHERE BINARY_CHECKSUM(' +
wh.for_x_in(@non_pk_cols,'ins.$',DEFAULT) +
') -- all other columns
!=
BINARY_CHECKSUM(' +
wh.for_x_in(@non_pk_cols,'incur.$',DEFAULT) +
')
;
/* ... and then update current state to new state */
UPDATE cur
SET ' +
wh.for_x_in(@sec_pk_col,'$ = new.$',DEFAULT) + ', ' +
@meta_ts + ' = GETDATE(), ' +
wh.for_x_in(@non_pk_cols,'$ = new.$',DEFAULT) + '
FROM ' + @table_curr + ' AS cur
INNER JOIN
#insert_ids AS ins
ON ' + wh.for_x_in(@pk_column,'cur.$ = ins.$','AND') + '
INNER JOIN
' + @insert_table_name + ' AS new
ON ' + wh.for_x_in(@pk_column,'ins.$ = new.$','AND') + '
;
/* Drop TEMP table #insert_ids */
DROP TABLE IF EXISTS #insert_ids;
';
IF (@debug = 'Y') BEGIN
PRINT @sql;
END;
EXECUTE (@sql);
-- Updated record, ID already known AND meta_valid_from is THE SAME as in ...__curr
-- So, update ...__curr
-- SQL injection: yah-dee-yah...
-- UPDATE CURRENT RECORD
SET @sql = N' UPDATE incur
SET ' + @meta_ts + ' = GETDATE(), ' +
wh.for_x_in(@non_pk_cols,'$ = ins.$',DEFAULT) + '
FROM ' + @table_curr + ' AS incur
INNER JOIN
' + @insert_table_name + ' AS ins
ON ' +
-- ..._id - so, id MUST already exist!
wh.for_x_in(@pk_column,'ins.$ = incur.$','AND') +
' AND ' +
wh.for_x_in(@sec_pk_col,'ins.$ = incur.$','AND') + ' -- meta_valid_from EQUAL TO current state
WHERE -- all other columns
BINARY_CHECKSUM(' + wh.for_x_in(@non_pk_cols,'ins.$',DEFAULT) + ')
!=
BINARY_CHECKSUM(' + wh.for_x_in(@non_pk_cols,'incur.$',DEFAULT) + ')
;
';
IF (@debug = 'Y') BEGIN
PRINT @sql;
END;
EXECUTE (@sql);
-- Historical record, ID already known AND historically also already known (so: update historical table)
-- Update historical state to newly-delivered historical state
-- SQL injection: yah-dee-yah...
-- UPDATE HISTORY
SET @sql = N' UPDATE inhist
SET ' + @meta_ts + ' = GETDATE(), ' +
wh.for_x_in(@non_pk_cols,'$ = ins.$',DEFAULT) + '
FROM ' + @insert_table_name + ' AS ins
INNER JOIN
' + @table_curr + ' AS incur
ON ' +
-- ..._id - so, id MUST already exist!
wh.for_x_in(@pk_column,'ins.$ = incur.$','AND') +
' AND '+
-- meta_valid_from BEFORE current state
wh.for_x_in(@sec_pk_col,'ins.$ < incur.$','AND')+ ' -- meta_valid_from BEFORE current state, so historical
INNER JOIN
' + @table_hist + ' AS inhist
ON ' +
wh.for_x_in(@pk_column,'ins.$ = inhist.$','AND') + ' AND '+
-- meta_valid_from exactly as in __hist-table!
wh.for_x_in(@sec_pk_col,'ins.$ = inhist.$','AND') + ' -- meta_valid_from EQUAL to historical state
WHERE BINARY_CHECKSUM(' + wh.for_x_in(@non_pk_cols,'ins.$',DEFAULT) + ')
!=
BINARY_CHECKSUM(' + wh.for_x_in(@non_pk_cols,'inhist.$',DEFAULT) + ')
;
';
IF (@debug = 'Y') BEGIN
PRINT @sql;
END;
EXECUTE (@sql);
-- Historical record, ID already known, but historically not already known (so: meta_valid_from is in the past, but not already in database --> insert)
-- unknown HISTORICAL
SET @sql = N' INSERT INTO ' + @table_hist + ' (' + @pk_column + ', ' + @sec_pk_col + ', ' + @meta_ts + ', ' + @non_pk_cols + ')
SELECT ' +
wh.for_x_in(@pk_column,'ins.$',DEFAULT) + ', ' +
wh.for_x_in(@sec_pk_col,'ins.$',DEFAULT) + ', ' +
wh.for_x_in(@meta_ts,'ins.$',DEFAULT) + ', ' +
wh.for_x_in(@non_pk_cols,'ins.$',DEFAULT) + '
FROM ' + @insert_table_name + ' AS ins
INNER JOIN
' + @table_curr + ' AS cur
ON ' +
wh.for_x_in(@pk_column,'ins.$ = cur.$','AND') + ' AND ' + -- ..._id
wh.for_x_in(@sec_pk_col,'ins.$ < cur.$','AND') + ' AND ' +
wh.for_x_in(@sec_pk_col,'ins.$ IS NOT NULL','AND') + '
WHERE ' +
wh.for_x_in(@pk_column,'ins.$ IS NOT NULL','AND') + ' AND
NOT EXISTS (SELECT 1
FROM ' + @table_hist + ' AS inhist
WHERE ' +
-- xxx_id
wh.for_x_in(@pk_column,'ins.$ = inhist.$','AND') +
' AND ' +
-- meta_valid_from
wh.for_x_in(@sec_pk_col,'ins.$ = inhist.$','AND') + '
)
;';
IF (@debug = 'Y') BEGIN
PRINT @sql;
END;
EXECUTE (@sql);
-- Completely new record, ID NOT NULL and not already in ...__curr --> insert!
-- Do we want to be able to enter a non-pre-existing ID?
SET @sql = N' INSERT INTO ' + @table_curr + ' (' + @pk_column + ', ' + @sec_pk_col + ', ' + @meta_ts + ', ' + @non_pk_cols + ')
SELECT ' +
@pk_column + ', ' +
@sec_pk_col + ', ' +
@meta_ts + ', ' +
@non_pk_cols + '
FROM ' + @insert_table_name + ' AS ins
WHERE ' +
wh.for_x_in(@pk_column,'$ IS NOT NULL','AND') + ' AND
NOT EXISTS (SELECT 1
FROM ' + @table_curr + ' AS incur
WHERE ' + wh.for_x_in(@pk_column,'ins.$ = incur.$','AND') + '
)
;'
IF (@debug = 'Y') BEGIN
PRINT @sql;
END;
EXECUTE (@sql);
-- As (a) non-missing ID(s) has/have been given, the corresponding SEQUENCES may have to be updated to reflect these new IDs.
SET @sql = N' DECLARE @next_val INT = 1;
DECLARE @curr_val INT = 1;
DECLARE @seq_id INT;
DECLARE @pk_col VARCHAR(128);
DECLARE @crsr CURSOR;
SET @crsr = CURSOR FOR SELECT CONVERT(INT,SUBSTRING(VALUE,1,CHARINDEX(''#'',VALUE)-1)) AS id,
SUBSTRING(VALUE,CHARINDEX(''#'',VALUE)+1, LEN(VALUE) - CHARINDEX(''#'',VALUE)) AS pk_column
FROM STRING_SPLIT(''' + @seq_id_arr + ''','','')
OPEN @crsr;
FETCH NEXT FROM @crsr INTO @seq_id, @pk_col;
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @inception NVARCHAR(500) = '''';
SET @inception = N''SELECT @next_v = COALESCE(MAX('' + @pk_col + ''),0) + 1
FROM ' + @table_curr + '
;'';
EXECUTE sp_executesql @inception , N''@pk_col VARCHAR(128), @next_v INT OUTPUT'', @pk_col = @pk_col, @next_v = @next_val OUTPUT;
SELECT @curr_val = CAST(CURRENT_VALUE AS INT)
FROM SYS.sequences
WHERE object_id = @seq_id
;
IF (CONVERT(INT,@next_val) > CONVERT(INT,@curr_val)) BEGIN
IF (@debug = ''Y'') BEGIN
PRINT(''MAX(id) found ('' + OBJECT_NAME(@seq_id) + '') to be greater than NEXT VALUE() ---> adjusted, now OK!'');
END;
SET @inception = N''ALTER SEQUENCE '' + OBJECT_SCHEMA_NAME(@seq_id) + ''.'' + OBJECT_NAME(@seq_id) + '' RESTART WITH '' + LTRIM(@next_val);
EXECUTE(@inception);
END
ELSE BEGIN
IF (@debug = ''Y'') BEGIN
PRINT(''Sequence '' + OBJECT_NAME(@seq_id) + '' is still adequate'');
END;
END;
;
FETCH NEXT FROM @crsr INTO @seq_id, @pk_col;
END;
CLOSE @crsr;
DEALLOCATE @crsr;
';
IF (@debug = 'Y') BEGIN
PRINT @sql;
END;
EXECUTE sp_executesql @sql , N'@debug CHAR(1)', @debug = @debug;
-- Completely new record, ID is NULL --> insert!
SET @sql = N' INSERT INTO ' + @table_curr + ' (' + @sec_pk_col + ', ' + @meta_ts + ', ' + @non_pk_cols + ')
SELECT ' +
wh.for_x_in(@sec_pk_col,'ins.$',DEFAULT) + ', ' +
wh.for_x_in(@meta_ts,'ins.$',DEFAULT) + ', ' +
wh.for_x_in(@non_pk_cols,'ins.$',DEFAULT) + '
FROM ' + @insert_table_name + ' AS ins
WHERE ' + wh.for_x_in(@pk_column,'$ IS NULL','AND') + '
;';
IF (@debug = 'Y') BEGIN
PRINT @sql;
END;
EXECUTE (@sql);
END
;