all groups > sql server programming > november 2007 >
You're in the

sql server programming

group:

parameters for where clause for stored procedure



parameters for where clause for stored procedure maa
11/3/2007 5:31:00 AM
sql server programming: I have parameters to a stored procedure that may be null or have a value. In
the case of a null value the search is not to be using that parameter. Is
there a way in the where clause to define it so the where clause can take a
parameter what ever its value and for the null not restrict the search.
Company does not like dynamic SQL so that is not an option. Other option is
copy and paste with different tests.

Example:

where( date=@DateTimeParameter )

I have tried adding a boolean result but it is not being short circuited:

where( 1=1 OR date=@DateTimeParameter )

Thanks,

Re: parameters for where clause for stored procedure Dan Guzman
11/3/2007 7:46:40 AM
[quoted text, click to view]

One method is to use COALESCE:

WHERE [date]=COALESCE(@DateTimeParameter, [date])

For a more thorough discussion of this and other techniques, see
http://www.sommarskog.se/dyn-search.html.

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]
AddThis Social Bookmark Button