merge語句能夠替換oracle匿名模塊,根據源數據與目標數據進行比較,源數據可以是一個sql語句、一個表、視圖、物化視圖
可以對目的表添加、修改、刪除
必配matched就執行 不必配就執行 有點像decode/case when then 但是它比這些複雜得多
但是有些寫法需要注意
在update語句時,update的字段不能包含匹配條件的字段
例如:
merge into merge_1 t
using thd_stkstockout t1
on(t.id=t1.id)
when matched then --條件成立就更新
update set t.code=t1.code
when not matched then
insert(id,code) values(t1.id,t1.code);---條件不成立就插入
它會報錯
正確:
merge into merge_1 t
using thd_stkstockout t1
on(t.id=t1.id)
when matched then
update set t.code=t1.code
when not matched then
insert(id,code) values(t1.id,t1.code);
詳細的請看ask tom:http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:5318183934935