日期:2014-05-19  浏览次数:20445 次

当提示:违反了PRIMARY KEY 约束 。不能在对象 '表tab1' 中插入重复键。我想知道是哪条数据违反了
表tab1
  字段1   name
  字段2   address
name是主键  

表tab2表结构与表tab1表结构一致
我将tab2的数据导入到表tab1中
insert   into   tab1   select   *   from   tab2
tab2.name有可能在tab1中有
当提示   违反   primary   key   约束...   不能插入重复键时
我想知道是哪条数据重复了,就是想得到name的值!

------解决方案--------------------
select a.* from tab1 a ,tab2 b where a.name = b.name
------解决方案--------------------
就是找tab1,tab2都有的数据:

这样也可以

select * from tab1 a where exists(select 1 from tab2 where a.name = name)
------解决方案--------------------
楼上的就可以了
------解决方案--------------------
select a.* from tab1 a inner join tab2 b
on a.name = b.name