Known issue?: SQL Server 2005 stored procedure fails to complete with a parameter

后端 未结 2 1182
闹比i
闹比i 2021-01-06 11:09

Your basic SP with a default parameter:

ALTER PROCEDURE [usp_debug_fails]
    @DATA_DT_ID AS int = 20081130
WITH RECOMPILE
AS
BEGIN
    /*
        Usage:
            


        
相关标签:
2条回答
  • 2021-01-06 11:16

    Try masking the input parameter.

    I guess the recompile isn't working because of the specified default (EDIT: Or parameter sent on first call) being sniffed at compile time. So, recompile has no effect.

    I've seen huge difference between estimated plans simply by changing the default from say, zero to NULL, or not having one.

    ALTER PROCEDURE [usp_debug_mightwork]
        @DATA_DT_ID AS int = 20081130
    AS
    BEGIN
        DECLARE @IDATA_DT_ID AS int
        SET @IDATA_DT_ID = @DATA_DT_ID
        -- Stuff here that depends on IDATA_DT_ID
    END
    

    I think this article explains...

    ...parameter values are sniffed during compilation or recompilation...

    EDIT:

    New link on query plans and parameters. It's still parameter sniffing whether a default is specified or not.

    The WITH RECOMPILE option specified on the GetRecentSales stored procedure above does not eliminate the cardinality estimation error

    Kind of related article about constants and plans

    0 讨论(0)
  • 2021-01-06 11:30

    Prevent parameter sniffing or you are toast when statistics change. I have 500+ sps and all of them start with:

    DECLARE @_Param1 ..., @_ParamN

    --- prevent pameter sniffing
    SELECT @_Param1 = @Param1, @_ParamN = @ParamN

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