SQL query to find duplicate rows, in any table

后端 未结 4 1886
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 18:12

I\'m looking for a schema-independent query. That is, if I have a users table or a purchases table, the query should be equally capable of catchin

4条回答
  •  执念已碎
    2021-01-18 18:45

    I'm still confused about what "detecting them might be" but I'll give it a shot.

    Excluding them is easy

    e.g.

    SELECT DISTINCT * FROM USERS
    

    However if you wanted to only include them and a duplicate is all the fields than you have to do

    SELECT 
       [Each and every field]
    FROM
       USERS
    GROUP BY
       [Each and every field]
    HAVING COUNT(*) > 1  
    

    You can't get away with just using (*) because you can't GROUP BY * so this requirement from your comments is difficult

    a schema-independent means I don't want to specify all of the columns in the query

    Unless that is you want to use dynamic SQL and read the columns from sys.columns or information_schema.columns

    For example

    DECLARE @colunns nvarchar(max)
    SET  @colunns = ''
    
    SELECT @colunns = @colunns  + '[' +  COLUMN_NAME  +'], ' 
    FROM INFORMATION_SCHEMA.columns  
    WHERE table_name = 'USERS'
    
    SET  @colunns  = left(@colunns,len(@colunns ) - 1)
    
    
    DECLARE @SQL nvarchar(max)
    SET @SQL = 'SELECT '  + @colunns 
              + 'FROM  USERS' + 'GROUP BY ' 
              + @colunns 
               + ' Having Count(*) > 1'
    
    
    exec sp_executesql @SQL
    

    Please note you should read this The Curse and Blessings of Dynamic SQL if you haven't already

提交回复
热议问题