欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

sqlserver清除完全重復的數(shù)據(jù)只保留重復數(shù)據(jù)中的第一條

 更新時間:2014年07月23日 16:43:41   投稿:whsnow  
根據(jù)autoID刪除臨時表#tmp中的重復數(shù)據(jù),只保留每組重復數(shù)據(jù)中的第一條
--創(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;

相關文章

最新評論