Want to add some conditions in MERGE statement in oracle sql for insert/update

后端 未结 3 1228
一个人的身影
一个人的身影 2021-02-04 04:48

I have to insert/update some RECORDS in table target_table. These records are coming one source_table.

I am using MERGE for update/insert in target_table. Query is as b

相关标签:
3条回答
  • 2021-02-04 04:54

    Instead Try Doing As below:

    MERGE
    INTO  target_table tgt
    USING source_table src
    ON  (src.column1 = tgt.column1)
    WHEN MATCHED
    THEN
    UPDATE
    SET   tgt.column3= src.column3,
         tgt.column4 = src.coulmn4  
    WHERE src.column1 = tgt.column1 and
         src.column2 = tgt.column2
    WHEN NOT MATCHED
    THEN
    INSERT ( tgt.column1,
         tgt.column2,
         tgt.column3,
         tgt.column4 )
    VALUES ( src.coulmn1,
         src.coulmn2,
         src.coulmn3,
         src.coulmn4);
    
    0 讨论(0)
  • 2021-02-04 04:55

    you can use this article for your question by pinal Dava

    MERGE statement

    0 讨论(0)
  • 2021-02-04 05:01

    You can simply add WHERE clause to UPDATE. More about it in oracle docs.

    So in your case it should look like:

    ...
    WHEN MATCHED
    THEN
       UPDATE
       SET   tgt.column3= src.column3,
             tgt.column4 = src.coulmn4
       WHERE tgt.column3 IN (val1, val2) 
    WHEN NOT MATCHED
    ...
    
    0 讨论(0)
提交回复
热议问题