重复插入数据

hunter19841209 2017-11-07 04:19:28
情况是插入数据时候发现有重复主键,违反唯一性约束,无法写入
请问各位大神有什么办法可以跳开这些重复的数据继续插入数据进去,或者把重复的数据直接覆盖过去。跪求~
...全文
252 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
以学习为目的 2017-11-22
  • 打赏
  • 举报
回复
 
insert into   tb1   select   tb2.*    from  tb2
   where tb2.id  not in  (select  id  from tb1 )

以学习为目的 2017-11-22
  • 打赏
  • 举报
回复
1楼和6楼都可以实现
sz_jack 2017-11-18
  • 打赏
  • 举报
回复
如果还有判断已经有是否重复,有了就更新,没有就插入 建议使用 类似 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);
自由自在_Yu 2017-11-08
  • 打赏
  • 举报
回复
引用 8 楼 yuxiangaaaaa 的回复:
[quote=引用 7 楼 yuxiangaaaaa 的回复:] 就是按6#的这种,插入b表前先判断一下数据是否已经存在就行了
https://www.cnblogs.com/dongsheng/p/4384754.html merge...into 执行前先判断一下
自由自在_Yu 2017-11-08
  • 打赏
  • 举报
回复
引用 7 楼 yuxiangaaaaa 的回复:
就是按6#的这种,插入b表前先判断一下数据是否已经存在就行了
https://www.cnblogs.com/dongsheng/p/4384754.htmlmerge...into 执行前先判断一下
自由自在_Yu 2017-11-08
  • 打赏
  • 举报
回复
就是按6#的这种,插入b表前先判断一下数据是否已经存在就行了
minsic78 2017-11-07
  • 打赏
  • 举报
回复
引用 5 楼 minsic78 的回复:
[quote=引用 4 楼 hunter19841209 的回复:] 可能之前描述不太清楚,现在遇到的问题是,想用表A的数据插入表B,但是表A的数据中有表b主键重复的数据,插入表B时候报错,想有什么办法可以排除掉重复的数据,或者忽略掉重复的数据
这个意思? select b.owner, b.object_name from b where not exists (select 1 from a where b.object_id = a.object_id); [/quote] 不对,拷贝错SQL了 应该是这个: insert into b select a.* from a where not exists (select 1 from b where b.id = a.id)
minsic78 2017-11-07
  • 打赏
  • 举报
回复
引用 4 楼 hunter19841209 的回复:
可能之前描述不太清楚,现在遇到的问题是,想用表A的数据插入表B,但是表A的数据中有表b主键重复的数据,插入表B时候报错,想有什么办法可以排除掉重复的数据,或者忽略掉重复的数据
这个意思? select b.owner, b.object_name from b where not exists (select 1 from a where b.object_id = a.object_id);
hunter19841209 2017-11-07
  • 打赏
  • 举报
回复
可能之前描述不太清楚,现在遇到的问题是,想用表A的数据插入表B,但是表A的数据中有表b主键重复的数据,插入表B时候报错,想有什么办法可以排除掉重复的数据,或者忽略掉重复的数据
minsic78 2017-11-07
  • 打赏
  • 举报
回复
你这要的不是insert,而是merge吧?
碧水幽幽泉 2017-11-07
  • 打赏
  • 举报
回复
提问的方式和思路
碧水幽幽泉 2017-11-07
  • 打赏
  • 举报
回复

题目的方式和思路有待大大加强啊。

我这里假设表为test,主键字段为id,insert来源表为tmp

insert into test
select *
from tmp a
left join test b on b.id = a.id
where b.id is null

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧