Oracle DB equivalent of on duplicate key update

后端 未结 3 1389
闹比i
闹比i 2020-12-06 06:52

I need to execute the following MySQL-query in Oracle:

INSERT INTO users VALUES(1,10) ON DUPLICATE KEY UPDATE points = 10;

Is there somethi

相关标签:
3条回答
  • 2020-12-06 07:02

    If you don't want to use MERGE, you can try:

    begin
       INSERT INTO users VALUES(1,10);
    
       exception
          when dup_val_on_index then
             update users
                set points = 10
              where id = 1;
    end;
    
    0 讨论(0)
  • 2020-12-06 07:23
    MERGE INTO users u
    USING (SELECT 1 as id FROM dual) a
    ON a.id = u.id
    WHEN MATCHED THEN UPDATE SET u.points = 10
    WHEN NOT MATCHED THEN INSERT (id, points) VALUES (1, 10);
    
    0 讨论(0)
  • 2020-12-06 07:24

    You would need to use a MERGE. Something like

    MERGE INTO users dest
      USING( SELECT 1 user_id, 10 points FROM dual) src
         ON( dest.user_id = src.user_id )
     WHEN MATCHED THEN
       UPDATE SET points = src.points
     WHEN NOT MATCHED THEN
       INSERT( user_id, points ) 
         VALUES( src.user_id, src.points );
    
    0 讨论(0)
提交回复
热议问题