tags:

views:

47

answers:

4

In my application I have a form that has some filter fields. None are required, if the user does enter a value I need to provide those as parameters in a where clause.

I don't know how to handle this in my stored procedure though.

for example the following stored procedure:

--parameters
@customername varchar(50),

SELECT * from ORDERS
WHERE customername = @customername

so if the user enters a Customername then of course it will return all with that customer name, if the user does not enter a user name it needs to return everything. How can I do this in this statement? is there a wildcard I can pass if the user didn't enter a customer name? Or do I need a separate procedure. I hope that makes sense, thanks!

+3  A: 
SELECT * from ORDERS 
WHERE customername = @customername or @customername is null
Russ
Using `Coalesce` or `IsNull` would be better
Chad
@Chad Why? I'm not saying you are wrong, but what evidence do you have? Statements without evidence are worthless.
Dan Diplo
Using one of those two will make future maintenance easier. Technically, it will probably evaluate to very similar query plans.
StingyJack
@Dan Diplo, just simpler to read `ISNULL(@customername, customername)` than it is to see two conditions with an `or`. Especially when you have several conditions, and start having to use brackets. I wasn't saying you were wrong either, your way works, it can just get confusing as the query gets larger.
Chad
@Chad It wasn't my answer! I was just curious about why it might be better.
Dan Diplo
@Dan Diplo, my bad, didn't look at the names that closely.
Chad
+3  A: 

Try this

--parameters
@customername varchar(50) = NULL,

SELECT * from ORDERS
WHERE customername = COALESCE(@customername, customername)

Per the comment that you need to return customernames that are null, This is integrated with Russ'.

SELECT * from ORDERS
WHERE (customername = COALESCE(@customername, customername)
 OR customername IS NULL)
StingyJack
Using your example, if the user does not enter a customer name, would I provide the stored proc just a null value for the @customername parameter?
twal
@twal. You could provide a null value manually, but since StingyJack's sproc here has NULL as the default value for that parameter, you could also just not provide that parameter at all.
Jon Hanna
oh duh I am an idiot I didn't see the @customername varchar(50) = NULL,
twal
OK This is very close. The issue I am having now is, if I don't provide a customer name, it returns everything except for the rows where customername is a null value (which unfortunately there are many in our DB).
twal
Yeah... Null != Null.
StingyJack
I need to return everything back even if no customer name is provided even those with null values in the customer name, as well as those with values as customer name. If the user doesn't enter a customer name then i need to get back the same results as if I was doing SELECT * from Orders
twal
use this in conjunction with Russ' to get null's in the return set
StingyJack
Thank you StingyJack. This is working great now! I appreciate your help. Have a great day!
twal
A: 

You could write an if statement in your stored proc. Like this.

IF @customername is null
BEGIN
  SELECT * FROM ORDERS
END
ELSE
BEGIN
  SELECT * FROM ORDERS WHERE customername = @customername
END
mpenrow
BAD BAD, that's just going to become a VERY messy proc
Chad
Difficult to maintain, and akin to dynamic SQL.
StingyJack
My example has only one where clause, when i write my production stored proc it could have 6 or 7 which could get messy. Thanks for answering though mpenrow.
twal
I agree this would get very messy with more than one filter. Don't use it. Use IsNull or Coalesce
mpenrow
+1  A: 

See this link for some ideas, as well as the pros and cons of different approaches to conditional where statements:

http://www.sqlservercentral.com/articles/T-SQL/61918/

NYSystemsAnalyst