I'm building a dynamic SQL statement in a MSSQL SP. After it's built, I want to set a variable to the only record it returns. Something like this:
SET @strCountSQL = 'SELECT COUNT(*) FROM (' + @strSQL + ') cnt'
SET @intRecordCountQY = EXEC(@strCountSQL)
Of course, it doesn't work - Incorrect syntax near the keyword 'EXEC'. - Does anyone know of a way to do something similar?
Yes, the query has to be built dynamically and no I can't call another stored procedure.
SET @strCountSQL = 'SELECT COUNT(*) FROM (' + @strSQL + ') cnt'
SET @intRecordCountQY = EXEC(@strCountSQL)
Of course, it doesn't work - Incorrect syntax near the keyword 'EXEC'. - Does anyone know of a way to do something similar?
Yes, the query has to be built dynamically and no I can't call another stored procedure.