Example: Specifying the Partitioning Column Set in the ON Clause When the Target Relation Has a Row-Partitioned Primary Index - Advanced SQL Engine - Teradata Database

SQL Data Manipulation Language

Product
Advanced SQL Engine
Teradata Database
Release Number
17.00
Published
September 2020
Language
English (United States)
Last Update
2021-01-23
dita:mapPath
qtb1554762060450.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1146
lifecycle
previous
Product Category
Teradata Vantage™

Consider the following table definitions where t1 has a primary index on a1 and is partitioned on column b1.

     CREATE TABLE t1 (
       a1 INTEGER,
       b1 INTEGER,
       c1 INTEGER)
     PRIMARY INDEX (a1)
     PARTITION BY b1;
     CREATE TABLE t2 (
       a2 INTEGER,
       b2 INTEGER,
       c2 INTEGER)
     PRIMARY INDEX (a2);

The following MERGE request is valid because it specifies the partitioning column, b1, of the target table, t1, in its ON clause:

     MERGE INTO t1
       USING t2
       ON a1=a2 AND b1=b2
     WHEN MATCHED THEN
       UPDATE SET c1=c2
     WHEN NOT MATCHED THEN
       INSERT (a2, b2, c2);

The following MERGE request aborts and returns an error message to the requestor because it does not specify the partitioning column of the target table, b1, in its ON clause:

     MERGE INTO t1
       USING t2
       ON a1=a2
     WHEN MATCHED THEN
       UPDATE SET c1=c2
     WHEN NOT MATCHED THEN
       INSERT (a2, b2, c2);

The following MERGE request aborts and returns an error message to the requestor because its INSERT specification orders columns b2 and c2 in a different sequence than they were specified in its ON clause. The INSERT specification must always match the ON clause constraints on the primary index of the target table, and its partitioning column set if the target table has row-partitioning.

     MERGE INTO t1
       USING t2
       ON a1=a2 AND b1=b2
     WHEN MATCHED THEN
       UPDATE SET c1=c2
     WHEN NOT MATCHED THEN
       INSERT (a2, c2, b2);

If the target table has row-partitioning, the values of the partitioning columns must also be specified in search_condition, and the INSERT clause must specify the same partitioning column values as search_condition.