user2182269 user2182269 - 5 months ago 61
SQL Question

Oracle alternative for mysql REPLACE INTO

In mysql we use

REPLACE INTO


to insert if row doesn't exist and to update if it exists.

Is there a corresponding command in Oracle?

Answer
MERGE
INTO    destTable d
USING   (
        SELECT  *
        FROM    sourceTable
        ) s
ON      (s.id = d.id)
WHEN NOT MATCHED THEN
INSERT  (id, destCol1, destCol2)
VALUES  (id, sourceCol1, sourceCol2)
WHEN MATCHED THEN
UPDATE
SET     destCol1 = sourceCol1,
        destCol2 = sourceCol2