Sunday 8 April 2012

DELETE THE DUPLICATE ROWS IN EMP

DELETE THE DUPLICATE ROWS IN EMP

This query is used to delete the duplicate rows in the any table:
 Before executing the query you must have the table with the name emptest the table structure is 
create table emptest(empno int,empName varchar(50),deptNo int ,sal money)
insert into emptest values(14,'xxx',10,10000)
insert into emptest values(13,'fsdaasf',11,20000)
insert into emptest values(10,'xcas',12,30000)
insert into emptest values(12,'d',10,40000)
insert into emptest values(11,'e',11,50000)
insert into emptest values(15,'f',12,60000)
insert into emptest values(16,'g',10,70000)

WITH Emp AS
(SELECT ROW_NUMBER ( ) OVER
 ( PARTITION BY empNo,empName,deptno,sal ORDER BY empNo ) AS RNUM FROM emptest )
DELETE FROM Emp WHERE RNUM > 1

No comments:

Post a Comment