sql中删除重复行(所有列的数据都是相同的只保留一条数据)


  1.要删除所有列都是相同的重复行,并且ID不是自动增长列,主键列,要想只保留一行数据,最简单有效的方法就是用临时表进行删除

 sql:

       select distinct * into #tmp from tablename

       drop table tablename

       select * into tablename from #tmp

       drop table #tmp

2.用游标进行删除

declare @max integer,@id integer
declare cur_rows cursor local for select 主字段,count(*) from tablename group by 主字段 having count(*) > 1
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from tablename where 主字段= @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM