update和merge只用于更新时,俩种方式是可互更换的
update适用于
1、更改单表速度快稳定性好;
2、某字段即是过滤条件又是更新字段,且该字段有选择性很强的索引时“update A set status=1 where id=1 and status=2 and idc in (表)”
merge适用于
1、根据一张表或多表联合查询的连接条件对另外一张表进行查询,连接条件匹配上的进行UPDATE,无法匹配的执行INSERT可直接用merge实现,执行效率要高于INSERT+UPDATE;
2、“update a set i=(select i from b where a.id=b.id) where not exists (select 1 from b where a.id=b.id)”not exists部分需要额外消耗,可以用merge避免;
merge不支持更改字段放入on【会报错 ORA-38104: 无法更新 ON 子句中引用的列: XX】
多表处理: update d d set (d.number)=( select i.number from i i where i.code in ( select code from d where number = '123456') ) where exists( select 1 from i i where d.code=i.code);
MERGE INTO i a
USING (SELECT i.number,i.code,i.name from i t,i i
where t.code=i.code and t.number <> i.number and t.type='01') c
ON (a.code=c.code)
WHEN MATCHED THEN
UPDATE SET a.number=c.number,a.name=c.name;