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

错误:ORA-27432:链的步骤不存在。

4

2 回答 2

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
于 2013-10-09T03:06:00.107 回答
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 直觉你的愿望,以前的答案可能会有一个好的行为)

于 2013-10-15T11:44:17.020 回答