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

删除多个重复记录

时间:2019-05-31 16:39:52      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:val   gif   end   数据   net   col   https   cte   处理   

博客中,Insus.NET有分享过处理重复记录的功能。如《删除重复Row记录数据https://www.cnblogs.com/insus/p/10890148.html

今天就改编这个,改为动态存储过程。日后可以针对任一张表来进行。

技术图片

 

技术图片
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Insus.NET
-- Blog:        https://insus.cnblogs.com
-- Create date: 2019-05-31
-- Update date: 2019-05-31
-- Description: 删除多个重复记录
-- =============================================
CREATE PROCEDURE [dbo].[usp_Delete_Multiple_Duplicate_Record] (
    @TABLE_NAME SYSNAME, 
    @Refer_Column_lists NVARCHAR(MAX) -- [a],[b],[c]
)    
AS
BEGIN    
    DECLARE @query NVARCHAR(MAX) = N    ;WITH cte_temp_table(rank_num,+ @Refer_Column_lists +)
    AS (
       SELECT ROW_NUMBER() OVER(PARTITION BY + @Refer_Column_lists + ORDER BY + @Refer_Column_lists +) AS rank_num, + @Refer_Column_lists +
       FROM + @TABLE_NAME +
    )
    DELETE FROM cte_temp_table WHERE rank_num > 1;
    
    EXECUTE sp_executeSql @query
END
Source Code

 

演示:

技术图片

 

技术图片
IF OBJECT_ID(tempdb.dbo.#Part) IS NOT NULL DROP TABLE #Part

CREATE TABLE #Part (
    [ID] INT,
    [Item] NVARCHAR(40)
)
GO
INSERT INTO #Part ([ID],[Item]) VALUES 
(23394,I32-GG443-QT0098-0001),
(45008,I38-AA321-WS0098-0506),
(14350,K38-12321-5456UD-3493),
(64582,872-RTDE3-Q459PW-2323),
(23545,098-SSSS1-WS0098-5526),
(80075,B78-F1H2Y-5456UD-2530),
(53567,PO0-7G7G7-JJY098-0077),
(44349,54F-ART43-6545NN-2514),
(36574,X3C-SDEWE-3ER808-8764),
(36574,RVC-43ASE-H43QWW-9753),
(14350,K38-12321-5456UD-3493),
(64582,872-RTDE3-Q459PW-2323),
(80075,B78-F1H2Y-5456UD-2530),
(53567,PO0-7G7G7-JJY098-0077),
(44349,54F-ART43-6545NN-2514),
(44349,54F-ART43-6545NN-2514),
(36574,X3C-SDEWE-3ER808-8764)
GO


EXECUTE [dbo].[usp_Delete_Multiple_Duplicate_Record] #Part,[ID],[Item]

SELECT * FROM #Part
Source Code

 

删除多个重复记录

标签:val   gif   end   数据   net   col   https   cte   处理   

原文地址:https://www.cnblogs.com/insus/p/10955759.html

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