Skip to content

Instantly share code, notes, and snippets.

@sureshnath
Last active December 15, 2015 03:29
Show Gist options
  • Save sureshnath/5194852 to your computer and use it in GitHub Desktop.
Save sureshnath/5194852 to your computer and use it in GitHub Desktop.
ORACLE SQL Merge : insert or update key value pair
--create table configuration(key varchar(50), val varchar(100));
merge INTO configuration t USING --- table name configuration
(SELECT
substr(keyVal,1, instr(keyVal,'=')-1) key
, substr(keyVal,instr(keyVal,'=')+1) val
----- add other columns
from (select 'key=val' keyVal FROM dual) d ------- replace key=val
) s ON ( s.key = t.key )
WHEN matched THEN
UPDATE
SET t.val = s.val
----- add other columns
WHERE t.val <> s.val WHEN NOT matched THEN
INSERT VALUES
(
s.key
, s.val
----- add other columns
)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment