使用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);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章