Why does this query return different results when in a stored procedure?

后端 未结 2 1405
北恋
北恋 2021-01-05 15:51

Synopsis

I\'ve got a query with a WHERE clause that contains a condition that checks a NULLable column against a parameter that may be NULL, like so:

相关标签:
2条回答
  • 2021-01-05 16:25

    add this line of code

    >   WITH RECOMPILE
    

    in the beggining of the procedure, it makes sure to build a new execution plan, not depending in the previous execution.

    0 讨论(0)
  • 2021-01-05 16:31

    Try altering the stored procedure in a session after setting ANSI_NULLS to ON.

    SET ANSI_NULLS ON
    GO
    
    alter procedure procedureName 
    ...
    

    From the docs:

    For stored procedures, SQL Server uses the SET ANSI_NULLS setting value from the initial creation time of the stored procedure. Whenever the stored procedure is subsequently executed, the setting of SET ANSI_NULLS is restored to its originally used value and takes effect. When invoked inside a stored procedure, the setting of SET ANSI_NULLS is not changed.

    0 讨论(0)
提交回复
热议问题