Created
February 16, 2020 15:10
-
-
Save konosp/de2e5cb539e988cf1ab9bbdbf58b552c to your computer and use it in GitHub Desktop.
BigQuery DML Merge Sample
This file contains hidden or 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 TABLE `<project id>`.`<dataset id>`.`test` | |
| OPTIONS() AS( SELECT 'a' AS id, '1' AS value ); |
This file contains hidden or 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
| MERGE INTO | |
| `<project id>`.`<dataset id>`.`test` AS destination_table | |
| USING | |
| ( | |
| WITH tmp2 AS ( | |
| SELECT 'a' AS id, '2' AS value | |
| UNION ALL | |
| SELECT 'b' AS id, '3' AS value | |
| ) SELECT * FROM tmp2 | |
| ) AS source_table | |
| ON destination_table.id = source_table.id | |
| WHEN MATCHED THEN UPDATE SET id = source_table.id, value = source_table.value | |
| WHEN NOT MATCHED THEN INSERT | |
| (id, value) VALUES (id, value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment