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

后端 未结 12 1587
南旧
南旧 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条回答
  •  眼角桃花
    2020-11-22 08:12

    A note regarding the two solutions that suggest:

    1) Insert, if exception then update,

    or

    2) Update, if sql%rowcount = 0 then insert

    The question of whether to insert or update first is also application dependent. Are you expecting more inserts or more updates? The one that is most likely to succeed should go first.

    If you pick the wrong one you will get a bunch of unnecessary index reads. Not a huge deal but still something to consider.

提交回复
热议问题