--創(chuàng)建測試表
CREATE TABLE [dbo].[testtab](
[id] [nchar](10) NULL,
[name] [nchar](10) NULL
) ;
--向測試表插入測試數(shù)據(jù)
insert into testtab values('1','1');
insert into testtab values('1','1');
insert into testtab values('2','2');
insert into testtab values('2','2');
insert into testtab values('3','3');
insert into testtab values('3','3');
--創(chuàng)建臨時表并向臨時表中插入測試表testtab中數(shù)據(jù)以及添加自增id:autoID
select identity(int,1,1) as autoID, * into #Tmp from testtab
--根據(jù)autoID刪除臨時表#tmp中的重復數(shù)據(jù),只保留每組重復數(shù)據(jù)中的第一條
delete #Tmp where autoID in(select max(autoID) from #Tmp group by id);
--清除testtab表中的所有數(shù)據(jù)
delete testtab;
--向testtab表中插入#Tmp表中被處理過的數(shù)據(jù)
insert into testtab select id,name from #Tmp;
--刪除臨時表#Tmp
drop table #Tmp;