Showing posts with label Remove Duplicate Rows from table. Show all posts
Showing posts with label Remove Duplicate Rows from table. Show all posts

Friday, October 19, 2012

Remove Duplicate Data from Sql-server 2005



First Declare a temp table
DECLARE @table Table(column1 INT, Column2 INT, total int)

insert duplicate data to temp table
insert INTO @table SELECT column1, Column2,count(*) FROM Catalogue.MailingListCategoryContacts GROUP BY column1, Column2 HAVING count(*) > 1

Delete from all the data from original table
DELETE FROM Catalogue.MailingListCategoryContacts WHERE column1 IN (SELECT column1 FROM @table)

again insert into the original table
INSERT INTO Catalogue.MailingListCategoryContacts(column1, Column2) SELECT column1, Column2 FROM @table