Skip to content

Instantly share code, notes, and snippets.

@cnukaus
Created December 11, 2022 22:46
Show Gist options
  • Save cnukaus/9015be429f5d12c120573b5efbf8b474 to your computer and use it in GitHub Desktop.
Save cnukaus/9015be429f5d12c120573b5efbf8b474 to your computer and use it in GitHub Desktop.
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
@cnukaus
Copy link
Author

cnukaus commented Dec 11, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment