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

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-04-03
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

The following examples fail because the requests reference tables other than the source or target table in 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. Table t3 is specified in the USING source table subquery, but 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);