How to detect duplicate rows in a SQL Server table?

前端 未结 5 621
渐次进展
渐次进展 2021-02-02 07:03

What is the most efficient way to detect duplicates in a 10 column / 50K row table? I\'m using MSSQL 8.0

5条回答
  •  无人共我
    2021-02-02 07:29

    To detect, just group by as Guge said.

    select fieldA, fieldB, count(*) from table
    group by fieldA, fieldB
    having count(*) > 1
    

    If you want to delete dupes... pseudo....

    select distinct into a temp table
    truncate original table
    select temp table back into original table
    

    With truncate you may run into problems if you have FK constraints, so be smart about dropping constraints and making sure you don't orphan records.

提交回复
热议问题