删除数据库中重复的记录
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
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
相关文章推荐
- 千万级数据库(MSSQL)删除重复记录
- Sqlserver 2000 数据库表中删除重复记录(仅保留一条)
- 查询和删除数据库表中有重复的数据,只保留唯一条记录的方法
- 删除数据库中重复记录
- 数据库中删除重复记录的一种简单的方法
- 从数据库中删除重复记录 SQL(MSSQL)
- 数据库重复记录删除
- 数据库删除完全重复和部分关键字段重复的记录
- MySQL 数据库中删除重复记录的方法总结
- 数据库删除重复记录的sql
- 删除数据库中重复的记录(保留一条)-oracle、mysql版
- 删除数据库中重复记录
- 数据库删除重复记录
- 实战day09(七)----删除数据库中的重复记录
- 删除数据库中重复的记录
- 数据库中查询及删除重复记录的方法大全
- 如何删除数据库中重复的记录
- MySQL语句删除数据库重复记录数据行
- 如何删除数据库中重复的记录
- 删除数据库中多余的重复记录(在mysql的数据库上测试过,成功删除)