使用公共密钥
使用另一个表中的数据更新一个表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
错误:ORA-27432:链不存在步骤。
答案 0 :(得分: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 :(得分: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直观了你的愿望,之前的答案可能会有很好的行为)