码迷,mamicode.com
首页 > 其他好文 > 详细

整理删除重复数据的语句

时间:2019-10-30 13:38:26      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:str   provider   ast   rom   nbsp   top   sql   declare   row   

开场白:前边一段为前期数据准备

后边t-sql为筛选出重复的行数-1,即为需要删除的数据,其中guid必须唯一

--前期准备
SELECT ProviderGUID,bankaccounts,count(1) as bum INTO #temp1 from p_ProviderBanks  group by ProviderGUID,bankaccounts 
having count(1)>1

SELECT rowid=IDENTITY(INT,1,1),flag=0, providerguid,a.ProviderBankGUID,a.BankAccounts INTO #temp2 FROM p_ProviderBanks a WHERE EXISTS (SELECT * FROM #temp1 WHERE a.ProviderGUID=ProviderGUID AND  a.BankAccounts=BankAccounts)

SELECT TOP (10-1) * FROM #temp1
SELECT rowid=IDENTITY(INT,1,1),flag=0,providerguid,BankAccounts,bum INTO #temp3  FROM #temp1
SELECT * FROM #temp2
SELECT * FROM #temp3
DROP TABLE #temp3


--删除重复数据
BEGIN 
SET NOCOUNT ON 
DECLARE @num INT 
DECLARE @rowid INT 
DECLARE @providerguid UNIQUEIDENTIFIER
DECLARE @bankaccounts VARCHAR(400) 
SELECT @rowid=MIN(rowid) FROM #temp3 WHERE flag=0
DECLARE @strsql NVARCHAR(200)
--CREATE TABLE erpbak.dbo.tempdelete 
--(
--providerbankguid UNIQUEIDENTIFIER
--)
PRINT @rowid
WHILE @rowid is not NULL
BEGIN
    SELECT @providerguid=ProviderGUID,@bankaccounts=BankAccounts,@num=bum FROM #temp3 WHERE  rowid=@rowid
    SET @num=@num-1
    --PRINT @bankaccounts
    --PRINT @num
    SET @strsql=    insert into erpbak.dbo.tempdelete 
    SELECT TOP +CAST( @num AS VARCHAR(5) )+  ProviderBankGUID FROM #temp2 WHERE BankAccounts=‘‘‘+CAST(@bankaccounts AS VARCHAR(100))+‘‘‘ AND ProviderGUID=‘‘‘+CAST(@providerguid AS VARCHAR(100))+‘‘‘‘
    --PRINT @strsql
    EXEC sp_executesql @strsql
    UPDATE #temp3 SET flag=1 WHERE rowid=@rowid
    SELECT @rowid=MIN(rowid) FROM #temp3 WHERE flag=0
END
SET NOCOUNT OFF 
END

 

整理删除重复数据的语句

标签:str   provider   ast   rom   nbsp   top   sql   declare   row   

原文地址:https://www.cnblogs.com/salv/p/11764067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!