一、概述
merge into操作的作用:判断是否符合条件后进行相应的update或者insert操作;
自带事务型命令,需要oracle版本大于10
二、分类
1、同表条件单判断,即源表和目标表是同一张。
merge into dest_table using (select :1 as accountid, :2 as userid from dual) t
on (s.accountid=t.accountid and s.userid=t.userid)
when matched then update set s.isfocused=:3
when not matched then insert (id, focustime,canceltime, isfocused, accountid, userid)
values(:4, :5, '', :6, :7, :8)
注意点:源表source_table 必须为dual,否则insert会失败;
2、不同表的条件对比
merge into dest_table
using (select accountid, userid from source_table where account='123' and userid='456' ) t
on (s.accountid=t.accountid and s.userid=t.userid)
when matched then update set s.isfocused=:1
when not matched then insert (id, focustime,canceltime, isfocused, accountid, userid)
values(:2, :3, '', :4, :5, :6)