在Oracle里,有时需要用一个表去更新另一个表。下面是实现的例子:
1.创建一个表test_a
create table test_a(
id number(19),
name varchar2(20),
age number(3)
)
2.自己随便插入一些数据
3.再创建一个与test_a一样结构的表test_b
create table test_b as
select * from test_a;
4.然后对test_b的数据做些修改,保证id的值至少有一个与test_a的id值相同
select * from test_b for update;
update test_b set age=age+1;
5最后用test_b表更新test_a表
update test_a a set (a.name,a.age)=
(select b.name,b.age from test_b b where a.id = b.id) where exists
(select * from test_b c where c.id=a.id)