Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何检测SQL Server表中的重复行?_Sql Server_Rdbms - Fatal编程技术网

Sql server 如何检测SQL Server表中的重复行?

Sql server 如何检测SQL Server表中的重复行?,sql-server,rdbms,Sql Server,Rdbms,检测10列/50K行表中重复项的最有效方法是什么?我使用的是MSSQL 8.0,您可以在所有列上使用groupby,然后使用count(*)1进行检测,就像Guge说的那样 select fieldA, fieldB, count(*) from table group by fieldA, fieldB having count(*) > 1 如果要删除重复项。。。伪 select distinct into a temp table truncate original table s

检测10列/50K行表中重复项的最有效方法是什么?我使用的是MSSQL 8.0,您可以在所有列上使用
groupby
,然后使用
count(*)1

进行检测,就像Guge说的那样

select fieldA, fieldB, count(*) from table
group by fieldA, fieldB
having count(*) > 1
如果要删除重复项。。。伪

select distinct into a temp table
truncate original table
select temp table back into original table
使用truncate时,如果有FK约束,可能会遇到问题,因此请明智地删除约束,并确保不会孤立记录。

尝试此方法

Select * From Table
Group By [List all fields in the Table here]
Having Count(*) > 1

要显示其他人所描述内容的示例,请执行以下操作:

SELECT
    Col1, -- All of the columns you want to dedupe on
    Col2, -- which is not neccesarily all of the columns
    Col3, -- in the table
    Col4,
    Col5,
    Col6,
    Col7,
    Col8,
    Col9,
    Col10
FROM
    MyTable
GROUP BY
    Col1,
    Col2,
    Col3,
    Col4,
    Col5,
    Col6,
    Col7,
    Col8,
    Col9,
    Col10
HAVING
    COUNT(*) > 1

除了提供的建议外,我还将继续努力防止将来出现重复,而不是稍后再尝试查找它们


这是使用假定为唯一的列(或列组)上的唯一索引来完成的。请记住,数据库中的数据可以从其他位置进行修改,而不是通过您正在使用的特定应用程序进行修改,因此最好在DB级别定义表中允许和不允许的内容。

这很有效,只要记住排除合成PK(如果有)