删除数据库重复数据一方法

今天遇到重复数据问题,我这里主要把主键自动增长去掉先,不然有这个"仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 '' 中为标识列指定显式值"问题。

创建表

Code

插入数据

Code

创建临时表

Code

这里测试Title重复的数据

--新建一个临时表的索引,并“忽略重复的值”,xxx作为唯一表识值字段
CREATE  UNIQUE  INDEX [temp] ON [dbo].[testchongfu_temp]([Title]WITH  IGNORE_DUP_KEY  ON [PRIMARY]
GO


--把原表的数据导入临时表,XXX为原表名
insert into testchongfu_temp Select * from testchongfu


--清空原表,并将临时表中的数据导回原表,最后删除临时表
delete testchongfu

insert into testchongfu select * from testchongfu_temp
drop table testchongfu_temp


 原参考地址:http://www.cnblogs.com/idotnet8/articles/1330768.html

原文地址:https://www.cnblogs.com/sendling/p/1374625.html