How to delete duplicate rows in SQL Server?

后端 未结 23 1840
长情又很酷
长情又很酷 2020-11-22 00:58

How can I delete duplicate rows where no unique row id exists?

My table is

col1  col2 col3 col4 col5 col6 col7
john  1          


        
23条回答
  •  执笔经年
    2020-11-22 01:46

    With reference to https://support.microsoft.com/en-us/help/139444/how-to-remove-duplicate-rows-from-a-table-in-sql-server

    The idea of removing duplicate involves

    • a) Protecting those rows that are not duplicate
    • b) Retain one of the many rows that qualified together as duplicate.

    Step-by-step

    • 1) First identify the rows those satisfy the definition of duplicate and insert them into temp table, say #tableAll .
    • 2) Select non-duplicate(single-rows) or distinct rows into temp table say #tableUnique.
    • 3) Delete from source table joining #tableAll to delete the duplicates.
    • 4) Insert into source table all the rows from #tableUnique.
    • 5) Drop #tableAll and #tableUnique

提交回复
热议问题