在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)