• hunchback (8/21/2013)


    Try adding OPTION (RECOMPILE) to the second query or use sp_executesql with parameters to do the comparison.

    SQL Server uses different approach to estimate cardinality when you use parameters or constants than when you use variables. The value of the variables are not known during compilation time.

    --

    AMB

    Just be cautious there because a statement recompile can use the value of a variable for sniffing purposes.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning