tencent cloud

Feedback

MERGE INTO

Last updated: 2024-08-07 17:26:15

    Description

    Supported engine: SparkSQL
    Applicable table: Native Iceberg tables and external tables
    Purpose: Update row data. This statement can replace the INSERT OVERWRITE statement.

    Statement

    MERGE INTO tablePrimary1 [ [ AS ] alias ]
    USING tablePrimary2
    ON booleanExpression
    [ WHEN MATCHED (AND matchedCond=booleanExpression)? THEN DELETE ]*
    [ WHEN MATCHED (AND matchedCond=booleanExpression)? THEN UPDATE SET assign [, assign ]* ]*
    [ WHEN NOT MATCHED (AND notMatchedCond=booleanExpression)? THEN INSERT VALUES '(' value [ , value ]*

    Parameter

    tablePrimary1: Table name in the three-part format. Example: catalog.database.table.
    alias: Alias
    tablePrimary2: Table name or subquery statement
    booleanExpression: Boolean expression

    Example

    MERGE INTO catalog1.db2.tbl1 t
    USING catalog1.db1.tbl1
    ON t.col1 = tbl1.col1
    WHEN MATCHED AND t.col1 = 14 THEN DELETE
    
    MERGE INTO catalog1.db2.tbl1 t
    USING (SELECT col1 FROM catalog1.db1.tbl1) s
    ON t.col1 = s.col1
    WHEN MATCHED AND t.col1 = 14 THEN UPDATE SET col1 = 2
    
    
    MERGE INTO catalog1.db2.tbl1 t
    USING (SELECT col1 FROM catalog1.db1.tbl1) s
    ON t.col1 = s.col1
    WHEN MATCHED AND t.col1 = 12 THEN UPDATE SET col1 = 0
    WHEN MATCHED AND t.col1 = 13 THEN UPDATE SET col1 = 1
    WHEN MATCHED AND t.col1 = 14 THEN UPDATE SET col1 = 2
    WHEN MATCHED AND t.col1 = 15 or s.col1 = 16 THEN UPDATE SET col1 = t.col1 + 1
    WHEN MATCHED AND t.col1 not in (12, 13, 14, 15) THEN UPDATE SET col1 = 4
    WHEN NOT MATCHED AND t.col1 = 12 THEN INSERT (col1) VALUES (s.col1)
    WHEN NOT MATCHED AND t.col1 = 13 THEN INSERT (col1) VALUES (s.col1 + 1)
    WHEN NOT MATCHED AND t.col1 = 14 THEN INSERT (col1) VALUES (s.col1 + 2)
    
    
    MERGE INTO catalog1.db2.tbl1 t
    USING (SELECT col1, col2 FROM catalog1.db1.tbl1) s
    ON t.col1 = s.col1
    WHEN MATCHED AND t.col1 = fun1(s.col2) THEN DELETE
    WHEN MATCHED AND t.col1 = db2.fun1(s.col2) THEN DELETE
    WHEN MATCHED AND (t.col1 = length(s.col2) or t.col1 = catalog2.db2.fun3(s.col2)) THEN UPDATE SET col1 = 3
    WHEN NOT MATCHED AND t.col1 = 12 THEN INSERT (col1) VALUES (db2.fun2(s.col2))
    
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support