SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Deleting Duplicate Rows

By Deepak Arjun, 2003/11/07

Total article views: 13070 | Views in the last 30 days: 109

To delete duplicate rows in Sql server is one of the tedious tasks. Duplicate rows might exist in a table because of bad database design or because constraints are not applied. Unlike Oracle where we get unique row-id for each row and using that we can delete duplicate records, there is no simple way to delete duplicate records in Sql Server.

One method used to delete duplicate records is to write a stored procedure, use cursor in that and delete row one by one. There is another simple interactive way to delete duplicate rows in Sql Server without using stored procedures and cursor.

I will use Enterprise manager for explanation but scripts can be used if duplicate records are to be deleted on regular basis from some table. Create one table OrderDetail with columns ShipmentId, OrderId, ArticleId and Quantity where ShipmentId, OrderId and ArticleId fields should be unique.

Add few duplicate records into this table.

Create a blank copy of table using:

Select * into OrderDetailCopy from OrderDetail where 1 = 0

Create Unique index on columns ShipmentId, OrderId and ArticleId. Also check Ignore duplicate key checkbox. Save the table.

Copy the records from OrderDetail into OrderDetailCopy using:

insert into OrderDetailCopy Select * from OrderDetail
You will get warning message:

Server: Msg 3604, Level 16, State 1, Line 1
Duplicate key was ignored.

Now you have OrderDetailCopy table without any duplicate rows.

Drop table OrderDetail using
Drop table OrderDetail

Rename table OrderDetailCopy to OrderDetail using

Sp_rename 'OrderDetailCopy','OrderDetail'

 

By Deepak Arjun, 2003/11/07

Total article views: 13070 | Views in the last 30 days: 109
Your response
 
 
Related tags

Basic Querying    
T-SQL    
 
Related content

ANSI Joins

By Neil Boyle | Category: Basic Querying
| 6,875 reads

Find The First Row

By Steve Jones | Category: Basic Querying
| 11,416 reads

Cast Your Data

By Steve Jones | Category: Basic Querying
| 15,230 reads
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com