您当前位置:优侠库 > 资讯攻略 >
来源:未知
2021-02-13 16:51:00 浏览: 分类:资讯攻略

SQL Server中删除重复数据的几个方法

在使用数据库的过程中,由于程序问题,有时会遇到重复数据,导致一些数据库设置不正确& hellip& hellip

方法一

以下是引用的内容:

Declare @ maxinteger,@ id integer
declare cur _ rows cursor local for select main field,count(*)from table name group by count(*)>的主字段。1
open cur _ rows
fetch cur _ rows into @ id,@ max
while @ @ fetch _ status = 0
begin
select @ max = @ max-1
setrowcount @ max
delete from table name where main field = @ id
fetch cur _ rows into @ id,@ max
end
close cur _ rows
set rowcount 0

方法2

重复记录有两种,一种是完全重复记录,即所有字段都重复,另一种是一些关键字段,比如姓名字段,而其他字段不一定重复或者可以忽略。

1.对于第一种重复,它更容易解决和使用

以下是引用的内容:

select distinct * from tableName

您可以获得没有重复记录的结果集。

如果该表需要删除重复记录(保留一条重复记录),可以按如下方式删除

以下是引用的内容:

select distinct * into # Tmp from tableName
drop table tableName
select * into tableName from # Tmp
drop table # Tmp

这种重复是由糟糕的表设计造成的,可以通过添加唯一的索引列来解决。

2.这种重复问题通常要求保留重复记录中的第一条记录。操作方法如下

假设重复的字段是姓名和地址,则需要得到这两个字段的唯一结果集。

以下是引用的内容:

select identity(int,1,1) as autoID,* into # Tmp from tableName
select min(autoID)as autoID into # Tmp 2 from # Tmp group by Name,autoID
select * from # Tmp where autoID in(select autoID from # Tmp 2)

最后一个select导致结果集中没有重复的名称和地址(但是添加了一个autoID字段,在编写时可以在select子句中省略它)

关键词:SQL Server

优侠库网站每天提供最新最热门的安卓手机软件、游戏下载、分享热门手游资讯攻略教程、手机软件教程步骤,专注安卓手机游戏软件下载。

本文链接:http://www.123down.cn/gonglue/227418.html

攻略资讯
安卓软件
安卓游戏