相当于重复密钥更新的 Oracle DB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10589350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:49:05  来源:igfitidea点击:

Oracle DB equivalent of on duplicate key update

sqloracle

提问by user1170330

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

我需要在 Oracle 中执行以下 MySQL 查询:

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

Is there something else besides merge? I just don't understand it.

除此之外还有别的merge吗?我就是不明白。

回答by Justin Cave

You would need to use a MERGE. Something like

您将需要使用MERGE. 就像是

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 );

回答by Sebas

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);

回答by archimede

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

如果您不想使用 MERGE,您可以尝试:

begin
   INSERT INTO users VALUES(1,10);

   exception
      when dup_val_on_index then
         update users
            set points = 10
          where id = 1;
end;