You are currently offline, waiting for your internet to reconnect

How to remove duplicate rows from a SQL Server table by using a script

Extended support for SQL Server 2005 ends on April 12, 2016

If you are still running SQL Server 2005 after April 12, 2016, you will no longer receive security updates and technical support. We recommend upgrading to SQL Server 2014 and Azure SQL Database to achieve breakthrough performance, maintain security and compliance, and optimize your data platform infrastructure. Learn more about the options for upgrading from SQL Server 2005 to a supported version here.

This article was previously published under Q70956
SUMMARY
You can use the following script to remove duplicate rows from a Microsoft SQLServer table:
   SELECT DISTINCT *      INTO duplicate_table      FROM original_table      GROUP BY key_value      HAVING COUNT(key_value) > 1   DELETE original_table      WHERE key_value      IN (SELECT key_value             FROM duplicate_table)   INSERT original_table      SELECT *         FROM duplicate_table     DROP TABLE duplicate_table				
When this script is executed, it follows these steps:
  1. It moves one instance of any duplicate row inthe original table to a duplicate table.
  2. It deletes all rows fromthe original table that also reside in the duplicate table.
  3. It moves therows in the duplicate table back into the original table.
  4. It drops the duplicate table.
MORE INFORMATION
This method is simple. However, it requires that you have sufficientspace available in the database to temporarily build the duplicatetable.
Windows NT
Properties

Article ID: 70956 - Last Review: 11/02/2007 09:48:54 - Revision: 4.3

Microsoft SQL Server 2005 Standard Edition, Microsoft SQL Server 2005 Express Edition, Microsoft SQL Server 2005 Workgroup Edition, Microsoft SQL Server 2005 Developer Edition, Microsoft SQL Server 2005 Enterprise Edition, Microsoft SQL Server 2000 Personal Edition, Microsoft SQL Server 2000 Standard Edition, Microsoft SQL Server 2000, Workgroup Edition, Microsoft SQL Server 2000 Developer Edition, Microsoft SQL Server 2000 Enterprise Edition, Microsoft SQL Server 7.0 Standard Edition, Microsoft SQL Server 7.0 Service Pack 1, Microsoft SQL Server 6.5 Standard Edition, Microsoft SQL Server 6.0 Standard Edition, Microsoft SQL Server 4.21a Standard Edition

  • kbprogramming KB70956
Feedback
html>ml>>p;did=1&t=">