合并语句的 BigQuery 等价物

BigQuery Equivalent of Merge Statement

我正在执行从 Teradata 到大查询的迁移。我遇到了在 USING 子句中包含 VALUES 的合并语句。

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
                          ); 

任何人都可以帮助我找到它的 BigQuery 等价物。

MERGE 语句用于更新 target table 使用 source table 和一个或多个主键(PK)。

根据 documentation,Teradata 和 BigQuery 的 MERGE 之间的区别是:

Teradata's MERGE operation is limited to matching primary keys within one access module processor (AMP). In contrast, BigQuery has no size or column limitation for MERGE operations, therefore using MERGE is a useful optimization. However, if the MERGE is primarily a large delete, see optimizations for DELETE elsewhere in this document.

DML scripts in BigQuery have slightly different consistency semantics than equivalent statements in Teradata. For example, Teradata's SET tables in session mode might ignore duplicates during a MERGE operation. For an overview on handling MULTISET and SET tables, snapshot isolation, and session and transaction handling, see the CREATE INDEX section elsewhere in this document.

在您的情况下,您似乎将 PK 用作 DL.department_idV.Run_Id。虽然,在 USING 子句内的语法中,您应该指定目标 table 而不仅仅是其字段。下面是语法,link:

MERGE target_name [[AS] alias]
USING source_name
ON merge_condition
#WHEN MATCHED 
#WHEN NOT MATCHED

因此,在您的情况下,语法将是:

MERGE dataset.department DL
USING (SELECT * FROM `project_id.dataset.source_table`) V
ON DL.department_id = V.Run_Id
WHEN MATCHED THEN
UPDATE SET DL.department_description = V.country
WHEN NOT MATCHED
#first specify the name of the columns in your then the values to insert
INSERT(colum1, column2, column3) VALUES(V.Run_Id, V.Country, V.curr)

请注意,在 INSERT 子句中,您首先指定要添加数据的列,然后在 VALUES 中插入要插入的值,您可以明确地写入值或从您的名称中命名列source_table 加上要添加的数据。我想指出的是,我认为 curr 是您来源 table 中的一个专栏。另外,您没有说明您的来源 table,只说明了它的一些字段。

为了进一步说明,下面是另一个例子

MERGE `dataset.target_table` T
USING (SELECT "New value" as value, "1" as ID) S
ON T.ID = S.ID
WHEN MATCHED THEN
UPDATE SET T.value_column = S.value
WHEN NOT MATCHED THEN
INSERT(value_column, id) VALUES("Value added", s.ID)

再次注意 INSERT 子句,首先描述目标 table 中的列,然后是将插入到 table WHEN NOT MATCHED 中的值.