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