title: Strange "Invalid column name" error when executing SP
body: <p>We have an SP in which we build a query and then execute the query by using the <code>exec(@select)</code> call, where the variable that holds the SQL query as it is constructed is <code>@select</code>.</p>
  
  <p>I have been assigned a request to add a new column to the resultset returned by the SP. This new column should not be returned in all circumstances, but only under a certain condition. Naturally, I added the following code to the SP.</p>
  
  <pre><code>IF @conditionIsMet
  BEGIN
  set @select =  @select + ", 'compQty' = convert(varchar(53), di.qty) "
  END
  </code></pre>
  
  <p>This worked fine until today, when my app keeps throwing a SQL exception with the message, "Invalid column name: compQty". This is erratic and the exception is not thrown always.</p>
  
  <p>Executing the SP in SQL Server Management Studio gives no errors. The column is rendered with the heading 'compQty'. So the app <em>should</em> pick up the column, but it doesn't seem to!!</p>
  
  <p>Can some one help?</p>