Delete duplicate records in SQL Server

Declare @tbl As table 
(
iid int,
iname varchar(10)
)  

insert into @tbl values(1,'A')
insert into @tbl values(2,'B')
insert into @tbl values(3,'C')
insert into @tbl values(4,'A')
insert into @tbl values(5,'C')

Select * from @tbl 

Delete From @tbl Where iid in (

Select iid from 
(Select * , ROW_NUMBER() Over ( partition by iname Order by iname ) As ICount  From @tbl)

As Qry Where Qry.ICount > 1
)

Select * from @tbl

Comments

Popular posts from this blog

Find procedure and function in SQL Server

Windows Service Install and Un Install in MS VisualStudio 2010

Find Store Procedures and Functions in MS SQL Server.