We have an SP in which we build a query and then execute the query by using the exec(@select)
call, where the variable that holds the SQL query as it is constructed is @select
.
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.
IF @conditionIsMet
BEGIN
set @select = @select + ", 'compQty' = convert(varchar(53), di.qty) "
END
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.
Executing the SP in SQL Server Management Studio gives no errors. The column is rendered with the heading 'compQty'. So the app should pick up the column, but it doesn't seem to!!
Can some one help?