Sub topic 11
MERGE with Conditional UPDATE, DELETE, and INSERT
This chapter assumes that you can write SELECT statements with WHERE, understand primary and foreign keys, and use COMMIT and ROLLBACK. It also assumes the emp_demo and emp_stage tables from the earlier examples.
MERGE combines an UPDATE and an INSERT. Oracle compares rows from a source table with rows in a target table. The ON condition decides whether each source row matches a target row.
- A matching row can be updated.
- A nonmatching row can be inserted.
- A matched row can also be deleted after its update.
WHEREclauses can make the update or insert conditional.
The basic conditional pattern
The following statement synchronizes emp_demo with emp_stage:
MERGE INTO emp_demo t
USING emp_stage s
ON (t.emp_id = s.emp_id)
WHEN MATCHED THEN
UPDATE SET t.salary = s.salary,
t.dept_id = s.dept_id,
t.status = s.status
DELETE WHERE (t.status = 'TERMINATED')
WHEN NOT MATCHED THEN
INSERT (emp_id, emp_name, dept_id, salary, status)
VALUES (s.emp_id, s.emp_name, s.dept_id, s.salary, s.status);
COMMIT;For every row in emp_stage, Oracle checks t.emp_id = s.emp_id.
- If the employee exists, Oracle updates the salary, department, and status.
- If the new status is
TERMINATED, Oracle deletes that matched row after applying the update. - If the employee does not exist, Oracle inserts the source row.
The DELETE WHERE condition uses the row after the update. This lets one MERGE move a row to a terminated state and remove it in the same statement.
Conditional updates and inserts
An update can have its own WHERE condition. For example, this updates only employees whose incoming salary is greater than the current salary:
MERGE INTO emp_demo t
USING emp_stage s
ON (t.emp_id = s.emp_id)
WHEN MATCHED THEN
UPDATE SET t.salary = s.salary,
t.status = s.status
WHERE s.salary > t.salary
WHEN NOT MATCHED THEN
INSERT (emp_id, emp_name, dept_id, salary, status)
VALUES (s.emp_id, s.emp_name, s.dept_id, s.salary, s.status);A matched row that fails the update WHERE clause remains unchanged. The insert branch does not run for that row because it already matched.
You can also restrict inserts:
MERGE INTO emp_demo t
USING emp_stage s
ON (t.emp_id = s.emp_id)
WHEN NOT MATCHED THEN
INSERT (emp_id, emp_name, dept_id, salary, status)
VALUES (s.emp_id, s.emp_name, s.dept_id, s.salary, s.status)
WHERE s.status <> 'TERMINATED';This ignores new terminated employees instead of inserting them.
> callout note > The ON condition must identify at most one target row for each source row. Duplicate source keys can cause ORA-30926, because Oracle cannot update the same target row consistently more than once. >
Choosing MERGE safely
Use MERGE when one source result must synchronize a target table. Before running it:
- Make the source unique on the columns used by
ON. - Test the source query with
SELECT. - Check the update and delete conditions separately.
- Review the affected rows before
COMMIT.
MERGE is one statement, but it still participates in the current transaction. Use ROLLBACK if the result is wrong before committing.
Exercise
In a MERGE statement, when does DELETE WHERE run?
Show answer
It runs for a matched row after the UPDATE branch changes the row. It can delete that row when the post-update values satisfy the delete condition.