Oracle: how to UPSERT (update or insert into a table?)

后端 未结 12 1386
南旧
南旧 2020-11-22 07:18

The UPSERT operation either updates or inserts a row in a table, depending if the table already has a row that matches the data:

if table t has a row exists          


        
12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 08:08

    I'd like Grommit answer, except it require dupe values. I found solution where it may appear once: http://forums.devshed.com/showpost.php?p=1182653&postcount=2

    MERGE INTO KBS.NUFUS_MUHTARLIK B
    USING (
        SELECT '028-01' CILT, '25' SAYFA, '6' KUTUK, '46603404838' MERNIS_NO
        FROM DUAL
    ) E
    ON (B.MERNIS_NO = E.MERNIS_NO)
    WHEN MATCHED THEN
        UPDATE SET B.CILT = E.CILT, B.SAYFA = E.SAYFA, B.KUTUK = E.KUTUK
    WHEN NOT MATCHED THEN
        INSERT (  CILT,   SAYFA,   KUTUK,   MERNIS_NO)
        VALUES (E.CILT, E.SAYFA, E.KUTUK, E.MERNIS_NO); 
    

提交回复
热议问题