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 the difference between upgrade and migration in sql server?

1036


What is replication with database mirroring? : sql server database administration

1003


What is an index in a database?

930


Do you know what is difference between stored procedure and user defined function?

1048


What gets stored inside msdb database?

1205


How to set the current database in ms sql server?

1024


You accidentally delete the msdb database what effect does this have on your existing sql databases, and how do you recover?

945


What does this statement do @@rowcount?

931


What is row_number function?

949


You want to check the syntax of a complicated update sql statement without executing it. What command should you use?

931


How can you control the amount of free space in your index pages?

1020


what are isolation levels? : Sql server database administration

926


What is user-defined multi-statement table-valued function?

1011


what's new in sql server 2016?

914


What is the new security features added in sql server 2016? : sql server security

1064