我們可以通過(guò)下述方法刪除重復(fù)記錄:
例:表名:dbo.品種描述$,字段包括:ID_PK,品種名稱(chēng),性狀標(biāo)準(zhǔn)編號(hào),代碼,首先創(chuàng)建一個(gè)和原表結(jié)構(gòu)一樣的表:
復(fù)制代碼 代碼如下:
select * into tmpA from dbo.品種描述$ where 1=2--創(chuàng)建完畢
在數(shù)據(jù)表中,品種名稱(chēng),性狀標(biāo)準(zhǔn)編號(hào)這兩個(gè)字段不能有重復(fù)值,執(zhí)行下述腳本:
declare @VarietyName nvarchar(255),
@StdCharCode nvarchar(255),
@iCount int
set @iCount=0;
declare insert_distinct_cursor cursor for
select 品種名稱(chēng),性狀標(biāo)準(zhǔn)編號(hào) from dbo.品種描述$ group by 品種名稱(chēng),性狀標(biāo)準(zhǔn)編號(hào)
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 (品種名稱(chēng),性狀標(biāo)準(zhǔn)編號(hào),代碼) select top 1 品種名稱(chēng),性狀標(biāo)準(zhǔn)編號(hào),代碼 from dbo.品種描述$ where 品種名稱(chēng)=@VarietyName and 性狀標(biāo)準(zhǔn)編號(hào)=@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