• Thanks, but option 1 is no good for us because we need to use stored procedures (your option 3). And that option results in the same problem. Using RECOMPILE in a sproc or query does not help. Option 2 (dynamic SQL) works fine, but is harder to develop and maintain.

    1) rewriting the query to use literals directly in queries instead of assigning them to variables and then using the variables in the queries

    2) using sp_executesql or API server cursors with parameters that replace your use of local variables

    3) using a stored procedure with parameters that replace your use of local variables