title: SQL - Using Variable as Column Name with count(@variable)
body: <p>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: </p>
  
  <p>Msg 164, Level 15, State 1, Line 8
  Each <code>GROUP BY</code> expression must contain at least one column that is not an outer reference.</p>
  
  <pre><code>declare @TheCount varchar(100)
  set @TheCount = 'Column_1'
  select @TheCount, count(@TheCount) as LineCount
  from staging
  group by @TheCount
  having count(@TheCount) &gt; 1
  order by 2 desc
  </code></pre>