您的位置:首页 > 数据库

删除数据库中重复的记录

2011-06-29 13:45 239 查看
查询重复列:
select * from tb_user where tb_name
in (select tb_name from tb_user group by tb_name having count(tb_name)>1)

删除数据库中部分字段重复的记录并保留一行。

--创建视图
create view v1 as
select *,row_number() over (order by serialcode) as 'id' from TMP_CQ_Cmn_History
where starttime in
(select starttime from TMP_CQ_Cmn_History where BranchNo=1 and SubCenterNo=5
and OperatorNo=2005 and CommentTime>'2010-01-30' group by starttime having count(starttime) >1 )
go
select * from v1

go

--定义游标
declare @id int
declare PJ cursor for select id from v1
open PJ
fetch next from PJ into @id WHILE (@@FETCH_STATUS=0)
begin
delete from v1 where id=1
fetch next from PJ into @id
end
CLOSE PJ
DEALLOCATE PJ

go
drop view v1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: