如何删除完全重复的列
2006-12-22 22:09
295 查看
利用游标实现
CREATE table userinfo
(
name char(10),
sex char(4),
phone char(10)
)
insert userinfo select 'aaa','男','12345' union all
select 'bbb','男','12345' union all
select 'aaa','男','12345' union all
select 'bbb','男','12345' union all
select 'aaa','男','12345' union all
select 'ccc','男','12345' union all
select 'aaa','男','1235'
--(1)定义
declare first cursor
for select count(*) as num ,name from userinfo
group by name
--(2)打开
open first
--(3)操作
declare @num int,@name varchar(10)
fetch next from first into @num,@name
while(@@fetch_status=0) --返回被 FETCH 语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。
--=0表示FETCH 语句成功
begin
declare second cursor for select * from userinfo where name=@name
open second
fetch next from second
while(@num >1)
begin
delete from userinfo where current of second
set @num=@num-1
fetch next from second
end
close second
deallocate second --删除游标引用
fetch next from first into @num,@name
end
close first
deallocate first
SELECT * FROM USERINFO
CREATE table userinfo
(
name char(10),
sex char(4),
phone char(10)
)
insert userinfo select 'aaa','男','12345' union all
select 'bbb','男','12345' union all
select 'aaa','男','12345' union all
select 'bbb','男','12345' union all
select 'aaa','男','12345' union all
select 'ccc','男','12345' union all
select 'aaa','男','1235'
--(1)定义
declare first cursor
for select count(*) as num ,name from userinfo
group by name
--(2)打开
open first
--(3)操作
declare @num int,@name varchar(10)
fetch next from first into @num,@name
while(@@fetch_status=0) --返回被 FETCH 语句执行的最后游标的状态,而不是任何当前被连接打开的游标的状态。
--=0表示FETCH 语句成功
begin
declare second cursor for select * from userinfo where name=@name
open second
fetch next from second
while(@num >1)
begin
delete from userinfo where current of second
set @num=@num-1
fetch next from second
end
close second
deallocate second --删除游标引用
fetch next from first into @num,@name
end
close first
deallocate first
SELECT * FROM USERINFO
相关文章推荐
- db2如何删除完全一样的重复数据
- db2如何删除完全一样的重复数据
- 如何删除表的重复行
- 如何在 Linux 中找到并删除重复文件
- oracle如何删除重复行
- Ubuntu如何完全删除JDK及重新安装
- 【ZT】SQL Server中如何删除Table中的重复行
- 如何删除数据表中的重复数据(各种数据库)
- Mac 基础教程:如何删除右键菜单重复项目
- sqlserver中如何删除没有主键的重复数据。
- 如何删除表中的重复记录只保留其中一条
- SQL删除完全重复数据
- Excel电子表格中如何做数据查找,重复数据删除,标记重复数据
- 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢?
- 如何在MSSQL删除多条重复数据
- 如何删除表中的重复记录只保留其中一条?
- 如何删除数据表中重复数据(二)
- 如何高效删除Oracle数据库中的重复数据
- SQL脚本--(通用存储过程)如何删除表中某个字段重复的数据
- 如何删除List中的重复元素