我需要在 Oracle 中执行以下 MySQL 查询:
INSERT INTO users VALUES(1,10) ON DUPLICATE KEY UPDATE points = 10;
除此之外还有别的merge
吗?我只是不明白。
您将需要使用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 );
如果您不想使用 MERGE,可以尝试:
begin
INSERT INTO users VALUES(1,10);
exception
when dup_val_on_index then
update users
set points = 10
where id = 1;
end;
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);