Example: Reference the Source or Target in the ON, WHEN MATCHED, or WHEN NOT MATCHED Clauses - Teradata Database - Teradata Vantage NewSQL Engine

SQL Data Manipulation Language

Product
Teradata Database
Teradata Vantage NewSQL Engine
Release Number
16.20
Published
March 2019
Language
English (United States)
Last Update
2019-05-03
dita:mapPath
fbo1512081269404.ditamap
dita:ditavalPath
TD_DBS_16_20_Update1.ditaval
dita:id
B035-1146
lifecycle
previous
Product Category
Teradata Vantage™

The following examples fail because they reference a table other than the source or target table in either their ON, WHEN MATCHED, or WHEN NOT MATCHED clauses.

The following example fails because table t4 is neither the derived source table s nor the target table t1.

     MERGE INTO t1
       USING (SELECT x2,y2, z3
              FROM t2, t3
              WHERE y2=10) AS s
       ON x1=y2 AND t4.x4=z2
     WHEN MATCHED THEN
       UPDATE SET z1=10
     WHEN NOT MATCHED THEN
       SELECT (y2, z2, x2);

The following example fails because table t3 is neither the derived source table s nor the target table t1. Even though t3 is specified in the USING source table subquery, it violates the restriction that only source and target tables can be referenced in an ON clause.

     MERGE INTO t1
       USING (SELECT x2,y2, z3
              FROM t2, t3
              WHERE y2=10) AS s
       ON x1=y2 AND t3.x4=z2
     WHEN MATCHED THEN
       UPDATE SET z1=10
     WHEN NOT MATCHED THEN
       INSERT (y2, z2, x2);