简体   繁体   English

合并语句的 BigQuery 等效项

[英]BigQuery Equivalent of Merge Statement

I am performing migration from teradata to Big query.我正在执行从 teradata 到大查询的迁移。 I have encountered a merge statement having VALUES in USING clause.我遇到了一个在 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
                          ); 

Can anyone help me out in finding what would be its BigQuery equivalent.任何人都可以帮助我找到它的 BigQuery 等价物。

The MERGE statement is used when you want to updated a target table using a source table with one or more primary keys(PK).当您想要使用具有一个或多个主键 (PK) 的source table来更新target table时,使用MERGE语句。

According to the documentation , the differences between Teradata's and BigQuery's MERGE are:根据文档,Teradata 和 BigQuery 的 MERGE 之间的区别是:

Teradata's MERGE operation is limited to matching primary keys within one access module processor (AMP). Teradata 的 MERGE 操作仅限于匹配一个访问模块处理器 (AMP) 中的主键。 In contrast, BigQuery has no size or column limitation for MERGE operations, therefore using MERGE is a useful optimization.相比之下,BigQuery 对 MERGE 操作没有大小或列限制,因此使用 MERGE 是一种有用的优化。 However, if the MERGE is primarily a large delete, see optimizations for DELETE elsewhere in this document.但是,如果 MERGE 主要是大型删除,请参阅本文档其他地方的 DELETE 优化。

DML scripts in BigQuery have slightly different consistency semantics than equivalent statements in Teradata. BigQuery 中的 DML 脚本的一致性语义与 Teradata 中的等效语句略有不同。 For example, Teradata's SET tables in session mode might ignore duplicates during a MERGE operation.例如,Teradata 在 session 模式下的 SET 表可能会在 MERGE 操作期间忽略重复项。 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.有关处理 MULTISET 和 SET 表、快照隔离以及 session 和事务处理的概述,请参阅本文档其他地方的 CREATE INDEX 部分。

In your case, it seems you are using the PK as DL.department_id and V.Run_Id .在您的情况下,您似乎将PK用作DL.department_idV.Run_Id Although, within your syntax inside the USING clause you should specify the targeted table not only its fields.虽然,在USING子句内的语法中,您应该指定目标表,而不仅仅是它的字段。 Below is the syntax, link :以下是语法, 链接

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

Therefore, in your case, the syntax will be:因此,在您的情况下,语法将是:

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)

Notice that within the INSERT clause you first specify the columns that data will be added then inside VALUES the values to be inserted, you can write the values explicitly or name the columns from your source_table with the data to be added.请注意,在INSERT子句中,您首先指定将添加数据的列,然后在VALUES中指定要插入的值,您可以显式写入值或使用要添加的数据命名source_table中的列。 I would like to point that, I considered curr as a column from your source table.我想指出的是,我认为curr是您源表中的一列。 Also, you did not stated your source table, only some of its fields.另外,您没有说明您的源表,只说明了它的一些字段。

For further clarification, below is another example为了进一步说明,下面是另一个例子

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)

Again, pay attention to the INSERT clause, first the columns from the target table are described then the values which will be inserted in the table WHEN NOT MATCHED .再次注意INSERT子句,首先描述目标表中的列,然后描述将在表中插入的值WHEN NOT MATCHED

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM