我們可以通過下述方法刪除重復記錄:
例:表名: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