Your basic SP with a default parameter:
ALTER PROCEDURE [usp_debug_fails]
@DATA_DT_ID AS int = 20081130
WITH RECOMPILE
AS
BEGIN
/*
Usage:
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
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