您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/3/10 9:36:38发布33次查看
所谓的重复记录就是某列数据有重复的,但是在实际的过程中,有可能您的需求是一列有重复,也可能两列有重复..根据实际情况,所得到的重复记录是有所不同的
我们可以通过下述方法删除重复记录:
例:表名: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
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product