Oracle Merge Statement
MERGE Statement
The MERGE statement can be used to conditionally insert or update data depending on its presence.
This method reduces table scans and can perform the operation in parallel. Consider the following example where data from the HR_RECORDS table is merged into the EMPLOYEES table.
MERGE INTO employees e
USING hr_records h ON (e.id = h.emp_id)
WHEN MATCHED THEN
UPDATE SET e.address = h.address
WHEN NOT MATCHED THEN
INSERT (id, address) VALUES (h.emp_id, h.address);
The source can also be a query.
MERGE INTO employees e
USING (SELECT *
FROM hr_records
WHERE start_date > ADD_MONTHS(SYSDATE, -1)) h ON (e.id = h.emp_id)
WHEN MATCHED THEN
UPDATE SET e.address = h.address
WHEN NOT MATCHED THEN
INSERT (id, address) VALUES (h.emp_id, h.address);
Comments
Post a Comment