I am performing migration from teradata to Big query. I have encountered a merge statement having VALUES in USING clause.
MERGE INTO department DL
USING VALUES
(
2,'ABC'
) AS V
(Run_Id, Country)
ON DL.department_id = V.Run_Id
WHEN MATCHED THEN
UPDATE SET
department_description = V.country
WHEN NOT MATCHED THEN
INSERT
(
V.Run_Id
, V.Country
curr
);
Can anyone help me out in finding what would be its BigQuery equivalent.
The MERGE statement is used when you want to updated a
target tableusing asource tablewith one or more primary keys(PK).According to the documentation, the differences between Teradata's and BigQuery's MERGE are:
In your case, it seems you are using the PK as
DL.department_idandV.Run_Id. Although, within your syntax inside the USING clause you should specify the targeted table not only its fields. Below is the syntax, link:Therefore, in your case, the syntax will be:
Notice that within the
INSERTclause you first specify the columns that data will be added then insideVALUESthe values to be inserted, you can write the values explicitly or name the columns from yoursource_tablewith the data to be added. I would like to point that, I consideredcurras a column from your source table. Also, you did not stated your source table, only some of its fields.For further clarification, below is another example
Again, pay attention to the
INSERTclause, first the columns from the target table are described then the values which will be inserted in the tableWHEN NOT MATCHED.