What is the new policy for deleting the duplicate records in SQL server.
Hi

Sometimes we create table without any primary key and duplicate records are entered in the table. When we have two records in the database with no difference what so ever it’s problematic to distinguish between rows. In these case the other row should be deleted. In SQL Server 2000 we have to use the Rowcount to limit the number of rows affected by the command.

[Note: Remember that the default value is 0, which means that there is no limit of the number of rows returned]

But we can change the value before using the command. We should also remember to change the value back after we are done with the command. To delete 2 rows with same record we need to set the value of Rowcount to 1 and issue a delete command for the record.

In SQL Server 2005 there is an enhancement for this purpose. We can use the top keyword with the delete command. Here is an example

DELETE TOP(1) FROM dbo.duplicateRecord WHERE ID = 1000

So in SQL Server2005 we should avoid the use of the Rowcount command and use the top keyword to delete duplicate records. Also Microsoft has said that the Using SET ROWCOUNT will not affect DELETE, INSERT, and UPDATE statements in the next release of SQL Server. Hence we should avoid using SET ROWCOUNT together with DELETE, INSERT, and UPDATE statements in any new development work, and plan to modify applications that currently use it.

Hope this helps
Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 2/5/2007 7:40:51 PM

how to delete duplicate records without creating new table. we not defined primary key. reply me soon

Posted on 5/17/2007 1:35:24 AM

The below query will help to delete the duplicate records

set rowcount 1

select * from table_name
while @@Rowcount > 0
Delete from table_name where columnname in (select distinct columnname from table_name group by columnname having count(*) > 1)

set rowcount 0

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia