Example: MERGE With Subquery - Analytics Database - Teradata Vantage

SQL Data Manipulation Language

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Analytics Database
Teradata Vantage
Release Number
17.20
Published
June 2022
Language
English (United States)
Last Update
2024-04-05
dita:mapPath
pon1628111750298.ditamap
dita:ditavalPath
qkf1628213546010.ditaval
dita:id
esx1472246586715
lifecycle
latest
Product Category
Teradata Vantage™

This example generalizes Example: Using MERGE to Update and Insert by using a subquery for source_table_reference rather than an explicit value list.

     USING (empno  INTEGER,
            salary INTEGER)
     MERGE INTO employee AS t
     USING (SELECT :empno, :salary, name
            FROM names
            WHERE empno=:empno) AS s(empno, salary, name)
       ON t.empno=s.empno
     WHEN MATCHED THEN UPDATE
      SET salary=s.salary, name = s.name
     WHEN NOT MATCHED THEN INSERT (empno, name, salary)
      VALUES (s.empno, s.name, s.salary);