有些表设计没有标识,没有主键,删除起来一直是个麻烦的事情,而CTE的运用却让这件事情变得简单。
create table CreDelete
(
id int identity(1,1),
name varchar(10)
)
go
insert into CreDelete
select '张三' as name
union all select '王2'
union all select '张三'
union all select '王2'
union all select '张1'
union all select '张2'
union all select '张1'
union all select '张2'
go
select * from CreDelete
;with mycte as
(
select ROW= ROW_NUMBER() over(PARTITION by name order by id),* from CreDelete
)delete from mycte where ROW>1
转载于:https://www.cnblogs.com/dingdingmao/archive/2012/03/30/3146491.html