删除多个重复记录

博客中,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

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

时间: 2024-10-16 22:01:48

删除多个重复记录的相关文章

删除表中重复记录大于两条保存两条

--删除表中重复记录大于两条保存两条delete from t_Cht_clm_reg F where F.case_id in (select T.case_id from (select B.* from (select e.* , row_number() over (partition by e.notice_id order by e.case_id desc) rn from t_Cht_clm_reg e) B where B.notice_id in(select A.notic

MySQL中删除多余的重复记录

检查重复记录 -- 检查重复code1 select count(identity) num, identity from event_log where code='code1' group by identity having count(identity) > 1 order by num desc 删除重复记录 DELETE FROM event_log WHERE `code`='code1' AND identity IN ( SELECT identity from ( SELEC

SqlServer删除表中重复记录

重复记录:有两个意义上的重复记录 一是完全重复的记录,也即所有字段均重复的记录: 二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略. 1.对于第一种重复,比较容易解决,使用 1 select distinct * from tableName 就可以得到无重复记录的结果集. 如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除 1 --查询无重复记录的结果集,并将结果集保存到临时表#Tmp 2 select distinct * into #T

160802、1.06s删除10w条重复记录只保留一条(mysql)及linux删除乱码文件

上次发表过的一遍删除重复记录的文章,其中最优的方案三删除1w条也花了0.07秒而2w条就已经花掉了4秒.今天进行了进一步优化,测试删除10w条只花了1.06秒.速度提升了很多. 建表语句 CREATE TABLE `test_user` ( `id` INT(10) NOT NULL DEFAULT '0', `namea` VARCHAR(40) NOT NULL, `nameb` VARCHAR(40) NOT NULL, `user_id` VARCHAR(40) NOT NULL, PR

删除表中重复记录

create table myTable1(id int primary key identity,col1 int,col2 int) DECLARE @i int;set @i=0;WHILE @i<10 BEGIN INSERT INTO myTable1 VALUES(@i,@i); INSERT INTO myTable1 VALUES(@i,@i); SET @i+=1; End --找到重复的记录SELECT col1,col2 from myTable1 GROUP BY col

rowid去重(删除表的重复记录)

-- 构造测试环境SQL> create table andy(id int,name varchar2(10));Table created.SQL>insert into andy values(1,'a');insert into andy values(2,'b');insert into andy values(3,'c');insert into andy values(4,'d');SQL> select * from andy; ID NAME---------- ---

使用oracle删除表中重复记录

(1)使用用rowid方法 查询重复数据:select * from person a where rowid !=(select max(rowid) from person b where a.cardid=b.cardid and a.pname=b.pname);        删除重复数据:delete from person a where rowid !=(select max(rowid) from person b where a.cardid=b.cardid and a.p

SQL 删除表中重复记录只剩一条

今天不小心插入数据时插入重复了 这表中没有任何主键外键,随便插入或删除 网上查了很多复杂的语句都没有解决问题 最后用delete top (1) 解决问题 delete top (1) from Persion where name = 'xiaomin' 也可以用 update top (1) from person where name ='xxxx' delete from person where name = 'xxxx' 没那么复杂

sql语句查询同一表内多字段同时重复的记录 sql数据库重复记录删除

分享下用sql语句删除数据库中重复记录的方法.比如现在有一人员表 (表名:peosons) 若想将姓名.身份证号.住址这三个字段完全相同的记录查询出来select p1.* from persons p1,persons p2 where p1.id<>p2.id and p1.cardid = p2.cardid and p1.pname = p2.pname and p1.address = p2.address可以实现上述效果.几个删除重复记录的SQL语句 1.用rowid方法2.用gr