oracle merge使用

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


發佈了28 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章