Sunday, November 1, 2009

Delete duplicate records from SQL Server Table

To delete the duplicate records(same records in one more time) from SQL Server Data base table,use this query.

First write the conditions in .cs file to delete (as insert, update, delete records) duplicate records in to Page.IsPostBack = true Property, for your reference here I have given sql query for deleting duplicate records.

SET ROWCOUNT 1
DELETE yourtable
FROM yourtable a
WHERE (SELECT COUNT(*) FROM yourtable b WHERE b.fname =
a.fname AND b.lname = a.lname) > 1
WHILE @@rowcount > 0
DELETE yourtable
FROM yourtable a
WHERE (SELECT COUNT(*) FROM yourtable b WHERE b.fname =
a.fname AND b. lname = a. lname) > 1
SET ROWCOUNT 0

At the page refresh / relode time this query will work and delete duplecate record. So in your database duplicate record will not be added.

No comments:

Post a Comment