Last active
December 15, 2015 03:29
-
-
Save sureshnath/5194852 to your computer and use it in GitHub Desktop.
ORACLE SQL Merge : insert or update key value pair
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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