How to delete duplicate rows in SQL Server?

后端 未结 23 1837
长情又很酷
长情又很酷 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:52

    Microsoft has a vey ry neat guide on how to remove duplicates. Check out http://support.microsoft.com/kb/139444

    In brief, here is the easiest way to delete duplicates when you have just a few rows to delete:

    SET rowcount 1;
    DELETE FROM t1 WHERE myprimarykey=1;
    

    myprimarykey is the identifier for the row.

    I set rowcount to 1 because I only had two rows that were duplicated. If I had had 3 rows duplicated then I would have set rowcount to 2 so that it deletes the first two that it sees and only leaves one in table t1.

    Hope it helps anyone

提交回复
热议问题