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语句,那么最好的方法是什么呢?
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语句,那么最好的方法是什么呢?
当前回答
关于两种解决方案的说明如下:
1)插入,如果异常则更新,
or
2)更新,如果sql%rowcount = 0,则插入
先插入还是先更新的问题也取决于应用程序。您是否期待更多的插入或更新?最有可能成功的应该先走。
如果你选错了,你会得到一堆不必要的索引读取。不是什么大问题,但仍然值得考虑。
其他回答
我想要Grommit的答案,除了它需要欺骗的值。我在可能出现一次的地方找到了解决方案:http://forums.devshed.com/showpost.php?p=1182653&postcount=2
MERGE INTO KBS.NUFUS_MUHTARLIK B
USING (
SELECT '028-01' CILT, '25' SAYFA, '6' KUTUK, '46603404838' MERNIS_NO
FROM DUAL
) E
ON (B.MERNIS_NO = E.MERNIS_NO)
WHEN MATCHED THEN
UPDATE SET B.CILT = E.CILT, B.SAYFA = E.SAYFA, B.KUTUK = E.KUTUK
WHEN NOT MATCHED THEN
INSERT ( CILT, SAYFA, KUTUK, MERNIS_NO)
VALUES (E.CILT, E.SAYFA, E.KUTUK, E.MERNIS_NO);
MERGE语句合并两个表之间的数据。使用双 允许我们使用此命令。请注意,这没有针对并发访问进行保护。
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
如果不存在,请插入 更新:
INSERT INTO mytable (id1, t1) SELECT 11, 'x1' FROM DUAL WHERE NOT EXISTS (SELECT id1 FROM mytble WHERE id1 = 11); UPDATE mytable SET t1 = 'x1' WHERE id1 = 11;
MERGE(“老式的方式”)的替代方案:
begin
insert into t (mykey, mystuff)
values ('X', 123);
exception
when dup_val_on_index then
update t
set mystuff = 123
where mykey = 'X';
end;
正如Tim Sylvester的评论所指出的那样,到目前为止给出的答案在并发访问面前都是不安全的,并且在竞争的情况下会引发异常。为了解决这个问题,必须将插入/更新组合包装在某种循环语句中,以便在出现异常时重新执行整个操作。
举个例子,下面是Grommit的代码如何被包装在一个循环中,以使其在并发运行时安全:
PROCEDURE MyProc (
...
) IS
BEGIN
LOOP
BEGIN
MERGE INTO Employee USING dual ON ( "id"=2097153 )
WHEN MATCHED THEN UPDATE SET "last"="smith" , "name"="john"
WHEN NOT MATCHED THEN INSERT ("id","last","name")
VALUES ( 2097153,"smith", "john" );
EXIT; -- success? -> exit loop
EXCEPTION
WHEN NO_DATA_FOUND THEN -- the entry was concurrently deleted
NULL; -- exception? -> no op, i.e. continue looping
WHEN DUP_VAL_ON_INDEX THEN -- an entry was concurrently inserted
NULL; -- exception? -> no op, i.e. continue looping
END;
END LOOP;
END;
注意:在SERIALIZABLE事务模式下,顺便说一句,我不推荐这种模式,你可能会遇到 ORA-08177:不能序列化此事务异常的访问。