Created
December 11, 2022 22:46
-
-
Save cnukaus/9015be429f5d12c120573b5efbf8b474 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE PROCEDURE 'proj.dataset.sp_name' () | |
BEGIN | |
DECLARE min_value INT64; | |
CREATE OR REPLACE TEMPORARY TABLE table_1 as | |
(select 1 as A); | |
-- INIT table build | |
CREATE TABLE IF NOT EXISTS ds.table_target | |
(id_1, STRING | |
, date_1, DATE | |
, int_1, INT64 | |
) | |
PARTITION by date_1; | |
-- MERGE current run reuslts into underlying table( insert new, update eisting for date matches, delete existing if no within date range | |
MERGE INTO tb.table_target t | |
using table_1 s | |
on t.id=s.id | |
AND t.date_1=s.date_1 | |
WHEN MATCHED THEN | |
UPDATE SET t.id_1=s.id_1 | |
WHEN NOT MATCHED THEN | |
INSERT ROW | |
WHEN NOT MATCHED BY SOURCE AND t.date_1 between min_date_1 and max_date_1 THEN | |
DELETE; | |
END | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.sqlservertutorial.net/sql-server-basics/sql-server-merge/