在Oracle數據庫裏,用一個表去更新另一個表的方法

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

發佈了18 篇原創文章 · 獲贊 1 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章