在 SQL Server 2008 中删除没有引用的重复记录

在本文中,将介绍如何在没有 SQL Server 主键的情况下从表中删除重复记录。

一旦创建了没有任何主键的表,用户就会输入两次相同的信息。 重复数据会产生很多问题,因为无法区分行。

有一种去除重复记录的方法例如:使用行号来区分重复数据。 设置 ID/SSN 的第一行号并删除剩余的行号。

创建一个名为 EmployeeDetails 的表并添加一些记录。

create table EmployeeDetails

(

 Id int,

 Name varchar(10),

 SSN varchar(12)

);

insert into EmployeeDetails

select 1, 'Jack', '123-55-9999' union all

select 2, 'oir', '123-56-9999'  union all

select 3, 'Jean', '123-57-9999' union all

select 5, 'Tim', '123-59-9999'  union all

select 6, 'Lisa', '123-70-9999' union all

select 1, 'Jack', '123-55-9999' union all

select 4, 'Mike', '123-58-9999' union all

select 5, 'Tim', '123-59-9999'  union all

select 6 ,'Lisa', '123-70-9999' union all

select 5, 'Tim', '123-59-9999'

删除重复记录使用行号区分重复数据,执行如下SQL语句:

delete abc from 
(select row_number() over (partition by id,name,ssn order by id ) tbl
from employeedetails) abc
where abc.tbl > 1
select * from employeedetails
日期:2020-06-02 22:18:05 来源:oir作者:oir