title: SQL Server 2005/2008 Group By statement with parameters without using dynamic SQL?
body: <p>Is there a way to write SQL Server Stored Procedure which to be strongly typed ( i.e. returning known result set of columns ) and having its group statement to be dynamic. </p>
  
  <p>Something like:</p>
  
  <pre><code>SELECT SUM( Column0 ) FROM Table1
  GROUP BY @MyVar
  </code></pre>
  
  <p>I tried the following also:</p>
  
  <pre><code>SELECT SUM( Column0 ) FROM Table1
  GROUP BY CASE @MyVar WHEN 'Column1' THEN Column1 ELSE Column2
  </code></pre>
  
  <p>The second statement works only in scenarios where the db types of Column1 and Column2 are the same. If they are not the SQL engine throws an error saying something similar to: <em>"Conversion failed when converting the nvarchar value 'SYSTEM' to data type [The Different TYPE]."</em></p>
  
  <p>What can I do to achieve strong result set and yet have some dynamic part - i.e. the grouper in my case? This will be exposed to LINQ.</p>
  
  <p>EDIT:</p>
  
  <p>As seems you can do it, but you should NOT! Absolutely overkill. Testing showed figures of thousand times slower execution plans. And it will only get slower with bigger result sets.</p>