I need to determine the duplicates within a large number of different columns. Rather than typing out each column name, I tried to use the following code. This is slightly more efficient because I only need to type the column name once. However, I get this error:
Msg 164, Level 15, State 1, Line 8
Each GROUP BY
expression must contain at least one column that is not an outer reference.
declare @TheCount varchar(100)
set @TheCount = 'Column_1'
select @TheCount, count(@TheCount) as LineCount
from staging
group by @TheCount
having count(@TheCount) > 1
order by 2 desc