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...

Delete duplicate rows from a table without primary key by
using a single query
Table Employee
empname salary
A 200
B 300
A 200
C 400
D 500
D 500

Output should be

A 200
B 300
C 400
D 500

Answer Posted / swati tripathi

declare myCursor cursor for
select empid
from employee
group by empid
having count(*) > 1

declare @EmpId int


OPEN myCursor;
FETCH NEXT FROM myCursor INTO @EmpId
WHILE @@FETCH_STATUS = 0
BEGIN

delete top(select count(*)-1 from employee where
empid=@EmpId) from employee where empid=@EmpId

FETCH NEXT FROM myCursor INTO @EmpId
END
close myCursor

deallocate myCursor

Is This Answer Correct ?    11 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a partitioned view?

949


Tell me what do you understand by a view? What does the with check option clause for a view do?

956


What are the advantages of partitioning?

1045


How can I add Reporting Services reports to my application?

125


What is delete query?

896


How to drop existing views from a database in ms sql server?

1039


What is proper subset of candidate key?

945


What is the use of for clause?

1034


How do you drop an index?

862


What is the template in sql?

916


Explain the creation and execution of a user-defined function in the sql server?

996


What are extended events in sql server?

903


How is table type constraint applied to a table?

932


Can we add a cpu to sql server?

958


What is the difference between the export /import functions in sql studio and standalone sql manager? : sql server management studio

1066