delete from 表 where 编码 in (select 编码 from 表 group by 编码 having count(*)>=2)
delete from 表 where 编码 in (select 编码 from 表 group by 编码 having count(*)>=2)把相同的都给删了~~
用一个while循环 while (exists(select Top 1 编码 from 表 group by 编码 having count(*)>=2)) Begin delete from 表 where 编码 in (select Top 1 编码 from 表 group by 编码 having count(*)>=2) End只删除重复的编号重复的重复项:),
刪除1筆得話,cjer() 這個就行
先distict然后全删除 再把distic的表更新原来的就行了
只留下最上面的一条,select Top 1 编码 from 表 group by 编码 having count(*)>=2 order by 编码 desc 在后头加个order by
select distinct * into #tmp from tabledelete from tableinsert into table select * from #tmp
select distinct * into #tmp from tabledelete from tableinsert into table select * from #tmp 是不行的,它编号相同,可其他值可能相同,所以还是会有编号相同的
根据cjer() 的改下--插入一个标识列 alter table 表 add ID int identity(1,1)--删除含重复编码的行,保留ID最上面的 delete from 表 where ID in( select ID from 表 where 编码 in (select 编码 from 表 group by 编码 having count(*)>=2) ) and ID not in ( select ID from 表 where 编码 in (select Top 1 编码 from 表 group by 编码 having count(*)>=2) )--删除ID列 alter tabe 表 drop column ID
有没有简单点,一句SQL能搞定的?重复的编码是完全一样的,只是其他列不一样。
你的情况是 总提示"键列信息不足或不正确,更新影响到多行吗?如果是可以这样:假设你有2条重复的数据,删除其中一条可以用 set rowcount 1 delete where... set rowcount 0 这样2条只会删除一条。set rowcount x的用法可以查查bookonline
while (exists(select Top 1 编码 from 表 group by 编码 having count(*)>=2))
Begin
delete from 表 where 编码 in (select Top 1 编码 from 表 group by 编码 having count(*)>=2)
End只删除重复的编号重复的重复项:),
是不行的,它编号相同,可其他值可能相同,所以还是会有编号相同的
如果是:先distinct到另一个表,清空原表,从另一个表把记录插回去就可以了,如果要如楼上说的用一句sql来完成,那我就不懂了。
如果否:也就是说编号相同但内容不同,那么就奇怪了,能只保留一条而删掉其他吗?其他的真的对业务没用了吗?谁知道要留下哪一条才是有用的?如果一定要这么做,那就用游标逐行扫重复编号的记录,逐条把他们删除掉。
alter table 表
add ID int identity(1,1)--删除含重复编码的行,保留ID最上面的
delete from 表
where ID in(
select ID from 表
where 编码 in
(select 编码 from 表 group by 编码 having count(*)>=2)
)
and ID not in
(
select ID from 表
where 编码 in
(select Top 1 编码 from 表 group by 编码 having count(*)>=2)
)--删除ID列
alter tabe 表
drop column ID
set rowcount 1
delete where...
set rowcount 0
这样2条只会删除一条。set rowcount x的用法可以查查bookonline