Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Write a query to delete duplicate records in SQL SERVER

Answer Posted / sumit

I have The Same Problem And I Have Done Woth This


DECLARE @empid int, @empname varchar(50),@Cnt int

DECLARE duplicate_cursor CURSOR FOR

-- select all columns in table bit you must have an count column
select empid,empname, count(*) Cnt
from tbl_Temp
group by empid, empname
Having count(*) > 1

OPEN duplicate_cursor

FETCH NEXT FROM duplicate_cursor
INTO @empid, @empname,@Cnt

WHILE @@FETCH_STATUS = 0
BEGIN

SET @Cnt = @Cnt - 1

SET ROWCOUNT @Cnt

DELETE tbl_Temp
WHERE @empid = empid AND @empname = empname


FETCH NEXT FROM duplicate_cursor
INTO @empid, @empname
END

CLOSE duplicate_cursor
DEALLOCATE duplicate_cursor

-- dont forget to set rowcount to 0
SET ROWCOUNT 0

Is This Answer Correct ?    10 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Difference between Sql server reporting services and Crystal reports?

168


How to create new tables with "select ... Into" statements in ms sql server?

981


What are indexers?

1079


Do you know what is user defined datatypes and when you should go for them?

959


What is the openxml statement in sql server?

1059


What is the default schema of your login session in ms sql server?

1040


Please explain that what are the basic functions for master, msdb, model, tempdb and resource databases? : SQL Server Architecture

961


How to use the inserted and deleted pseudo tables?

953


What is data modification?

952


How to find index size for each index on table?

995


What is a mixed extent?

1003


How to declare and use cursor variables?

1154


Define left outer join?

956


What does set rowcount do?

896


What is ems sql management studio? : sql server management studio

1095