刪除重復(fù)記錄,并且剩下一條
發(fā)布日期:2022-02-02 11:19 | 文章來(lái)源:源碼之家
例:表名:dbo.品種描述$,字段包括:ID_PK,品種名稱,性狀標(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ù)表中,品種名稱,性狀標(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 品種名稱,性狀標(biāo)準(zhǔn)編號(hào) from dbo.品種描述$ group by 品種名稱,性狀標(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 (品種名稱,性狀標(biāo)準(zhǔn)編號(hào),代碼) select top 1 品種名稱,性狀標(biāo)準(zhǔn)編號(hào),代碼 from dbo.品種描述$ where 品種名稱=@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
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章