三種在oracle裡更新資料的方式:
1.替換更新
case when 替換當一個字段為空時,用一個字段替換它
select case when a is null then b else a end as data From A
2.插入更新
一個表的資料插入另一個表
insert into table1 select * from table2
3.指定條件下更新指定列
3.1update簡單更新
update table1 set columnA=1 where columnB=100
3.2配合merge into大量更新
merge into table1 using
(select *from table2 x where x.rowid =
(select MAX(y.rowid) from table2 y
where x.columnA= y.columnA
and x.columnB=y.columnB))
table2 on(table1.columnA=table2.columnA
and table1.columnB=table2.columnB)
#【比對前提條件後形成1對1的關系】
when matched then update set
table1.column1=table2.column1,
table1.column2=table2.column2,
table1.column3=table2.column3,
table1.column4=table2.column4
注:在滿足table1的某幾列與table2某幾列完全相同時,能夠得到唯一的一行
table1的值,此時用table2 的内容去更新table1的指定列,一對一更新。
其中merge into最推薦使用,不易出錯,且更新速度快。