insert if not exists oracle

后端 未结 10 2082
天命终不由人
天命终不由人 2020-12-07 23:08

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert.

10条回答
  •  轮回少年
    2020-12-07 23:38

    It that code is on the client then you have many trips to the server so to eliminate that.

    Insert all the data into a temportary table say T with the same structure as myFoo

    Then

    insert myFoo
      select *
         from t
           where t.primary_key not in ( select primary_key from myFoo) 
    

    This should work on other databases as well - I have done this on Sybase

    It is not the best if very few of the new data is to be inserted as you have copied all the data over the wire.

提交回复
热议问题