Oracle MERGE INTO的使用
在时常操作数据库,我们会有一个批量的增量更新的动作。
一般来说,我们使用游标。
create cursor ....
然后读取每一条记录,看看表里面有没有记录,没有记录就插入,有记录就更新。
可是游标操作的效率让人凌乱。
?
于是乎,DB2和Oracle都支持的MERGE INTO语句发挥了用处。
?
先看一段代码。
merge into fares t using (select DEPART, ARRIVE, price from my_table) tmp on (t.depart = tmp.depart and t.arrive = tmp.arrive) when matched then update set t.price = tmp.price when not matched then insert values (tmp.depart, tmp.arrive, tmp.price) ;
?
?