- Create a source table with the following definition and containing a million rows:
CREATE volatile TABLE MergingTable_Source (
ID DECIMAL(18,0),
END_DT DATE,
SOLD_AMT int
)
UNIQUE PRIMARY INDEX (ID) on commit preserve rows;
- Create a target table with the following DDL:
CREATE volatile TABLE MergingTable_Target (
ID DECIMAL(18,0),
END_DT DATE,
SOLD_AMT int
)
UNIQUE PRIMARY INDEX (ID);
- We will insert in the following MergingTable_Target table if the ID from MergingTable_Source does not exist. And if it does, we will update the END_DT and SOLD_AMT columns:
MERGE INTO MergingTable_Target t
USING MergingTable_Source s
ON T.ID = S.ID
WHEN MATCHED THEN
UPDATE SET END_DT = S.END_DT ,
SOLD_AMT= S.SOLD_AMT
WHEN NOT MATCHED THEN
INSERT (S.ID,
S.END_DT,
S.SOLD_AMT);