title: Pass table and column names as parameters and retrieve value
body: <p>I am trying to built generic query to pass column name I want to count on and table name I want to select value.</p>
  
  <p>So far this is my code:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[GenericCountAll]
      @TableName VARCHAR(100),
      @ColunName VARCHAR(100)
  AS
  BEGIN
      DECLARE @table VARCHAR(30);
      DECLARE @Rowcount INT;
  
      SET @table = N'SELECT COUNT(' + @ColunName +') FROM ' + @TableName + '';
      EXEC(@table)
  
      SET @Rowcount = @@ROWCOUNT
      SELECT @Rowcount
  END
  </code></pre>
  
  <p>Trying  to execute like this:</p>
  
  <pre><code>EXEC GenericCountAll 'T_User', 'Id';
  </code></pre>
  
  <p>but looks like I get two results, first result always returning a value of 1, and the second result returns the real count. Can anyone take a look?</p>