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

后端 未结 12 1389
南旧
南旧 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 07:56

    An alternative to MERGE (the "old fashioned way"):

    begin
       insert into t (mykey, mystuff) 
          values ('X', 123);
    exception
       when dup_val_on_index then
          update t 
          set    mystuff = 123 
          where  mykey = 'X';
    end;   
    

提交回复
热议问题