2013-10-09 40 views
3

更新一個表中的數據與來自其他使用公共密鑰更新一個表從另一個數據使用公共密鑰

create table table1 (
    id varchar2(4), 
    name varchar2(10), 
    desc_ varchar2(10) 
) 


create table table2 (
    id varchar2(4), 
    id_new varchar2(4) 
) 

insert into table1 values('1111', 'a', 'abc') 
insert into table1 values('2222', 'b', 'def') 
insert into table1 values('3333', 'c', 'ghi') 
insert into table1 values('4444', 'd', 'jkl') 

insert into table2 values('1111', '8080') 
insert into table2 values('2222', '9090') 

merge into table1 t1 
using (select * from table2) t2 
on (t1.id = t2.id) 
when matched then update set t1.id = t2.id_new 

error: ORA-27432: step does not exist for chain .

+0

原因是'在ON子句中引用的列無法更新',您可以使用相同的無引用列進行合併 – ajmalmhd04

回答

2

這應該工作:

update table1 t1 
set id = coalesce((
    select id_new 
    from table2 t2 
    where t1.id = t2.id), id); 

下面是與merge另一種方法:

merge into table1 t1 
using (select * from table2) t2 
on (1 = 1) 
when matched then update set t1.id = t2.id_new where t1.id = t2.id 
1

你會獲得使用可能是最好的速度:

merge into table1 t1 
using (select t1.rowid rw, t2.id_new 
     from table2 t2 
     join table1 on (table1.id = t2.id) 
     ) s 
on t1.rowid = s.rowid 
when matched then update set t1.id = s.id_new; 

但是這取決於優化器(上如果CBO直覺,答案可能會有很好的表現它的願望)

相關問題