0

How the upsert command is different from update command and how it works please make me understand with example and syntax.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
Piyush Ranjan
  • 29
  • 1
  • 8
  • 3
    There's no `UPSERT` command, only `UPDATE ... ELSE INSERT ...`. It's a kind of predecessor of `MERGE`, see http://www.info.teradata.com/HTMLPubs/DB_TTU_15_00/index.html#page/SQL_Reference/B035_1146_015K/ch03.023.409.html – dnoeth Jan 09 '17 at 12:20

1 Answers1

4

UPSERT command inserts rows that don’t exist and updates the rows that do exist. In teradata, it can be achieved using MERGE or UPDATE-ELSE-INSERT.

UPDATE-ELSE-INSERT syntax:

    UPDATE department
SET budget_amount = 80000
WHERE department_number = 800 ELSE
  INSERT INTO department(department_number, department_name, budget_amount, manager_employee_number)
VALUES(800,
       'Test Dept',
       80000,
       NULL);

UPDATE-ELSE-INSERTis a teradata feature.

MERGE syntax:

MERGE INTO DEPARTMENT USING
VALUES(700,
       '    Test Department',
       80000) AS dept(dept_num, dept_name, budget_amt) ON dept.dept_num = department_number WHEN MATCHED THEN
UPDATE
SET budget_amount = dept.budget_amt WHEN NOT MATCHED THEN
INSERT
VALUES(dept_num,
       dept_name,
       budget_amt,
       NULL);

Hope this will help.

zarruq
  • 2,445
  • 2
  • 10
  • 19