当前位置: 移动技术网 > IT编程>数据库>MSSQL > 删除重复记录,并且剩下一条

删除重复记录,并且剩下一条

2017年12月12日  | 移动技术网IT编程  | 我要评论

中藏网,现货原油分析师总监龙泽解盘,工作感想范文

我们可以通过下述方法删除重复记录:
例:表名:dbo.品种描述$,字段包括:id_pk,品种名称,性状标准编号,代码,首先创建一个和原表结构一样的表:
复制代码 代码如下:

select * into tmpa from dbo.品种描述$ where 1=2--创建完毕
在数据表中,品种名称,性状标准编号这两个字段不能有重复值,执行下述脚本:

declare @varietyname nvarchar(255),
@stdcharcode nvarchar(255),
@icount int
set @icount=0;
declare insert_distinct_cursor cursor for
select 品种名称,性状标准编号 from dbo.品种描述$ group by 品种名称,性状标准编号
open insert_distinct_cursor
fetch next from insert_distinct_cursor into @varietyname,@stdcharcode
while (@@fetch_status <> -1)
begin
if (@@fetch_status <> -2)
begin
insert into dbo.tmpa (品种名称,性状标准编号,代码) select top 1 品种名称,性状标准编号,代码 from dbo.品种描述$ where 品种名称=@varietyname and 性状标准编号=@stdcharcode;
set @icount=@icount+1;
end
fetch next from insert_distinct_cursor into @varietyname,@stdcharcode
end

close insert_distinct_cursor
deallocate insert_distinct_cursor
print @icount

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网