Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favourite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved in your SSC briefcase from the favorites tab.
Download now (direct download link)

Deleting Duplicate Records

By Amitesh Kumar,

Deleting duplicate records:

Concept:

Self join the table on all the columns that can possibly have same values and differentiate b/w the duplicate values based on the identity column. In the provided script I've taken Employees table which has some duplicate rows. The duplicacy is possible on EmployeeNo and EmployeeId. Id is a Identity column. TO find and delete the duplicate records, I join the Employee table with itself on the non-unique columns, EmployeeNo and EmployeeId in this case.

Contraints:

The table in consideration must have at least one non-unique column. If a table does not have any identity column then there are two possiblities :

i. Use the get row_num sql-query in conjunction with this script in place of unique field.

ii.

* Create a copy of the table using:

Select * into EmployeesCopy from Employees where 1 = 0

* Alter table to add an identity column in it.

* Insert all records from Employees to EmployeesCopy.

* Delete duplicates using sbove script implementation

* Delete all records from Employees

* Copy the new contents from EmployeesCopy to Employees


Total article views: 2713 | Views in the last 30 days: 5
 
Related Articles
FORUM

Delete Duplicates from table with no identity column

delete duplicates from table with all columns with datatype varchar()

FORUM

delete duplicates

delete duplicates

FORUM

identity column

identity column

FORUM

Delete Duplicate Records,

How can i delete duplicate records without changing their Identity values.

FORUM

Identity column

Is identity column a good idea?

Tags
duplicate records    
rowid    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones