17.05 - Example: MERGE With Subquery - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL Data Manipulation Language

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
Release Date
January 2021
Content Type
Programming Reference
Publication ID
B035-1146-175K
Language
English (United States)

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);