在這種情況下,merge
是首選解決方案。它已經提供。
下面是使用連接更新語法(根據另一個發佈的答案,「Oracle不支持」)的解決方案。這裏提出不要與merge
解決方案競爭,只是爲了表明Oracle確實支持連接更新語法。
這裏是update
聲明:
update (select x.erncd as x_erncd, m.erncd as m_erncd
from xx_1 x inner join xx_map_1 m on x.xx_gtn_nbr = m.xx_gtn
)
set x_erncd = m_erncd;
這裏是一個競爭的會話來說明其工作原理。
create table xx_map_1 (xx_gtn number primary key, erncd varchar2(10));
insert into xx_map_1
select 21, 'A1' from dual union all
select 22, 'A2' from dual union all
select 23, 'BB1' from dual union all
select 34, 'AB1' from dual
;
select * from xx_map_1;
XX_GTN ERNCD
---------- ----------
21 A1
22 A2
23 BB1
34 AB1
create table xx_1 (xx_employee_id varchar2(20), xx_gtn_nbr number, erncd varchar2(10));
insert into xx_1
select '00001', 21, null from dual union all
select '00001', 23, null from dual union all
select '00120', 34, null from dual union all
select '00120', 37, null from dual union all
select '00120', 23, null from dual
;
select * from xx_1;
XX_EMPLOYEE_ID XX_GTN_NBR ERNCD
-------------------- ---------- ----------
00001 21
00001 23
00120 34
00120 37
00120 23
insert
聲明和結果:
update (select x.erncd as x_erncd, m.erncd as m_erncd
from xx_1 x inner join xx_map_1 m on x.xx_gtn_nbr = m.xx_gtn
)
set x_erncd = m_erncd;
select * from xx_1;
XX_EMPLOYEE_ID XX_GTN_NBR ERNCD
-------------------- ---------- ----------
00001 21 A1
00001 23 BB1
00120 34 AB1
00120 37
00120 23 BB1
謝謝,合併是答案。 –