情况是插入数据时候发现有重复主键,违反唯一性约束,无法写入
请问各位大神有什么办法可以跳开这些重复的数据继续插入数据进去,或者把重复的数据直接覆盖过去。跪求~

解决方案 »

  1.   


    题目的方式和思路有待大大加强啊。我这里假设表为test,主键字段为id,insert来源表为tmpinsert into test
       select * 
         from tmp a 
     left join test b on b.id = a.id
      where b.id is null
      

  2.   

    你这要的不是insert,而是merge吧?
      

  3.   

    可能之前描述不太清楚,现在遇到的问题是,想用表A的数据插入表B,但是表A的数据中有表b主键重复的数据,插入表B时候报错,想有什么办法可以排除掉重复的数据,或者忽略掉重复的数据
      

  4.   


    这个意思?select b.owner, b.object_name
      from b
     where not exists (select 1 from a where b.object_id = a.object_id);
      

  5.   


    这个意思?select b.owner, b.object_name
      from b
     where not exists (select 1 from a where b.object_id = a.object_id);
    不对,拷贝错SQL了 应该是这个:
    insert into b
      select a.*
        from a
       where not exists (select 1 from b where b.id = a.id)
      

  6.   


    https://www.cnblogs.com/dongsheng/p/4384754.htmlmerge...into 执行前先判断一下
      

  7.   


    https://www.cnblogs.com/dongsheng/p/4384754.html
    merge...into 执行前先判断一下
      

  8.   

    如果还有判断已经有是否重复,有了就更新,没有就插入
    建议使用 类似
    merge into hwicd.t_handle_reply_info a 
    using    t_tmp_handle_reply_info b
    on (a.handleid = b.handleid)
    WHEN MATCHED THEN   
    UPDATE SET  a.updatetime=b.updatetime,
    a.handleopinion =b.handleopinion
    WHEN NOT MATCHED THEN
      insert  ( a.handleid, 
                a.orderid, 
                a.inchargedept, 
                a.handleper, 
                a.handleopinion, 
                a.handledate, 
                a.state, 
                a.statetime, 
                a.createtime, 
                a.updatetime)
       VALUES (b.handleid, 
                    b.orderid, 
                    b.inchargedept, 
                    b.handleper, 
                    b.handleopinion, 
                    b.handledate, 
                    b.state, 
                    b.statetime, 
                    b.createtime, 
                    b.updatetime);
      

  9.   

     
    insert into   tb1   select   tb2.*    from  tb2
       where tb2.id  not in  (select  id  from tb1 )