UPSERT 操作更新或插入表中的行,具体取决于表是否已有与数据匹配的行:
if table t has a row exists that has key X:
update t set mystuff... where mykey=X
else
insert into t mystuff...
由于 Oracle 没有特定的 UPSERT 语句,那么执行此操作的最佳方法是什么?
The 合并语句 http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF01606合并两个表之间的数据。使用双
允许我们使用这个命令。请注意,这不受并发访问保护。
create or replace
procedure ups(xa number)
as
begin
merge into mergetest m using dual on (a = xa)
when not matched then insert (a,b) values (xa,1)
when matched then update set b = b+1;
end ups;
/
drop table mergetest;
create table mergetest(a number, b number);
call ups(10);
call ups(10);
call ups(20);
select * from mergetest;
A B
---------------------- ----------------------
10 2
20 1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)