I have a query that has a where clause built using a number of local variables, This however is painfully slow. Below is a rough example as I don\'t have access to the query cur
Using local variables at WHERE filter causes FULL TABLE SCAN. Because SS does not know the values of local variables at compile time. So it creates an execution plan for the largest scale that can be avaliable for the column.
In order to prevent the performance problem, SS must know the values of the variables at compile time. Defining a SP, and passing these local varibles as a parameter is one of the solution of the problem. Another solution is, using sp_executesql and passing those local variables as a parameter again...
Or you can add OPTION ( RECOMPILE ) at the end of your sql statement to make your local varibles be compiled. This will solve the performans problem.