2007年12月10日 星期一

Oracle MERGE Rows

MERGER INTO table_name table_alias
USING (table | view | sub_query ) alias
ON (join condition)
WHEN MATCHED THEN
UPDATE SET
col1=col_val1,
col2=col2_val
WHEN NOT MATCHED THEN
INSERT (column_list)
VALUES (column_values);

Example:

MERGER INTO copy_emp c
USING employees e
ON (c.employee_id = e.employee_id)
WHEN MATCHED THEN
UPDATE SET
c.first_name =e.first_name,
c.last_name = e.last_name,
.....
c.department = e.department
WHEN NOT MATCHED THEN
INSERT VALUES (e.employee_id, e.first_name, ...... , e.department);