merge into using on when then 是 Oracle 數據庫合并數據表的一種操作,
MERGE INTO target_table a
USING source_table b
ON (a.col1 = b.col1 and a.col2 = b.col2)
WHEN MATHED THEN update set a.col3 = b.col3
WHEN NOT MATHED THEN insert (a.col1,a.col2,a.col3) values(b.col1,b.col2,b.col3);
merge into ... 表示將數據合并到目標表中
using ... 表示使用哪個數據表進行合并
on ... 表示合并的條件
when ... then ... 表示合并時執行的操作
常見的 when 和 then 組合:
when matched then update:目標表有相同的記錄,更新該記錄的值
when not matched then insert:目標表不存在相同的記錄,插入新的記錄
when not matched by source then delete: 源表中不存在相同的記錄,刪除目標表中的記錄