使用merge into 来更新目标表的个别字段
--更新职业 5s merge into cr_gr x using ( select b.custid,c.new_grbh,b.occupation /*职业*/,nvl(d.new_bm,'90') new_occupation /*新职业*/ from cm002@to_db2 b left join scdy_grzhbh_dzb c on b.custid=c.old_grbh left join scdy_tmp_zgzy_dzb d on trim(b.occupation)=d.old_bm where occupation<>' ' ) new_tab on (x.grbh=new_tab.new_grbh ) when matched then update set x.zhiye=new_tab.new_occupation;
采用
--通过构造临时表test_source,把前后的对照关系找清楚,并且可观测到 merge into test_target using test_source on (test_source.id = test_target.id)--注意,如果是多条件,则 on后边一定要有个括号,把各条件都括进来 when matched then update set test_target.name = test_source.name when not matched then insert values(test_source.name,test_source.id);--最后一行可以省略
1、UPDATE或INSERT子句是可选的
2、UPDATE和INSERT子句可以加WHERE子句
3、在ON条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表
4、UPDATE子句后面可以跟DELETE子句来去除一些不需要的行
参考:https://blog.csdn.net/weixin_39734304/article/details/79182416
为了对比效果
采用其他的方式更新
1
update test_target a set a.name= ( with b as (select id,name from test_source ) select a.name from a where a.id=b.id )
2、创建中间的临时表
3、主表加索引
4、update语句
update cr_gr a set (a.zhiye,a.zhichen,a.jylx)=( select nvl(d.new_bm,'90'),nvl(b.techpost,'0'), nvl(d.new_bm,'90') from cm002@to_db2 b left join scdy_grzhbh_dzb c on b.custid=c.old_grbh left join scdy_tmp_zgzy_dzb d on b.occupation=d.old_bm where c.new_grbh=a.grbh);
测试语句 drop table t_stat; create table t_stat(a1 int,b1 int,C1 varchar2(20)); insert into t_stat select level,mod(level,1000), null from dual connect by level<=100000; commit; select count(1) from T_STAT t;--10万条数据 select * from t_stat; ---构造B表 drop table t_ref; create table t_ref(b1 int,C2 varchar2(20)); insert into t_ref select mod(level,1000),null from dual connect by level<=1000; update t_ref set c2='能更' where b1<=500; update t_ref set c2='' where b1>500; commit; ------------------开始更新 --方法1:1.3S merge into t_stat s using t_ref t on (t.b1 = s.b1) when matched then update set s.c1 = t.c2; ----验证 select * from t_stat; --清空 update t_stat set c1 = null; commit; ------------方法2 update ----2.7s update t_stat a set a.c1= ( select b.c2 from t_ref b where a.b1=b.b1 ); select * from t_ref