Oracle insert if not exists statement

后端 未结 5 795
长发绾君心
长发绾君心 2020-11-27 19:46
insert into OPT (email, campaign_id) values(\'mom@cox.net\',100)
where not exists( select * from OPT where (email =\"mom@cox.net\" and campaign_id =100)) ;
         


        
5条回答
  •  伪装坚强ぢ
    2020-11-27 20:45

    insert into OPT       (email,        campaign_id) 
    select 'mom@coxnet' as email, 100 as campaign_id from dual MINUS
    select                 email,        campaign_id from OPT;
    

    If there is already a record with mom@cox.net/100 in OPT, the MINUS will subtract this record from the select 'mom@coxnet' as email, 100 as campaign_id from dual record and nothing will be inserted. On the other hand, if there is no such record, the MINUS does not subract anything and the values mom@coxnet/100 will be inserted.

    As p.marino has already pointed out, merge is probably the better (and more correct) solution for your problem as it is specifically designed to solve your task.

提交回复
热议问题