SQL 刪除重複資料

有時整理SQL資料時需要刪除重覆資料,在這裡做個筆記。

1
2
3
4
5
6
7
8
set rowcount 1
WHILE 1=1
begin
delete from table where id in(select id from table group by id having count(*)>1)
if @@rowcount=0
break
end
set rowcount 0