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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章