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
Published
January 2021
Language
English (United States)
Last Update
2021-01-22
dita:mapPath
vnq1596660420420.ditamap
dita:ditavalPath
hoy1596145193032.ditaval

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