天天看点

oracle更新数据方式

三种在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最推荐使用,不易出错,且更新速度快。