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

我们可以通过下述方法删除重复记录:
例:表名:dbo.品种描述$,字段包括:ID_PK,品种名称,性状标准编号,代码,首先创建一个和原表结构一样的表:
<div class="codetitle"><a style="CURSOR: pointer" data="23992" class="copybut" id="copybut23992" onclick="doCopy('code23992')"> 代码如下:

<div class="codebody" id="code23992">
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_distinctcursor
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_distinctcursor INTO @VarietyName,@StdCharCode
END CLOSE insert
distinctcursor
DEALLOCATE insert
distinct_cursor
print @iCount

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...