为了账号安全,请及时绑定邮箱和手机立即绑定

Oracle:如何使用UPSERT(更新或插入到表中?)

Oracle:如何使用UPSERT(更新或插入到表中?)

慕田峪7331174 2019-07-02 16:02:23
Oracle:如何使用UPSERT(更新或插入到表中?)UPSERT操作更新或插入表中的一行,这取决于表是否已经有与数据匹配的行:if table t has a row exists that has key X:     update t set mystuff... where mykey=Xelse     insert into t mystuff...由于Oracle没有特定的UPSERT语句,那么最好的方法是什么?
查看完整描述

3 回答

?
芜湖不芜

TA贡献1796条经验 获得超7个赞

合并的另一种选择(“旧式方式”):

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;


查看完整回答
反对 回复 2019-07-02
?
红糖糍粑

TA贡献1815条经验 获得超6个赞

这个合并语句将数据合并到两个表之间。使用DUAL允许我们使用这个命令。请注意,这不受并发访问的保护。


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


查看完整回答
反对 回复 2019-07-02
  • 3 回答
  • 0 关注
  • 4059 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信