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语句,执行此操作的最佳方法是什么?
最佳答案
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;