all groups > sql server programming > december 2006 >
You're in the

sql server programming

group:

help with conditions in WHERE clause


RE: help with conditions in WHERE clause Nigel Rivett
12/31/2006 9:10:01 PM
sql server programming:
The case statement returns a value which you need to test
e.g.

WHERE
1 = CASE @ReturnTimedOrNotTimed
WHEN 'N' THEN case when PickupTime IS NULL then 1 else 0 end
WHEN 'U' THEN case when PickupTime IS NOT NULL then 1 else 0
end
END



[quoted text, click to view]
help with conditions in WHERE clause Keith G Hicks
12/31/2006 10:48:09 PM
In a stored procedure (sql 2k) I'm passing a parameter for whehter or not
the result set returns rows if a column is null.

@ReturnTimedOrNotTimed CHAR(1)

IF @ReturnTimedOrNotTimed = 'T'
SELECT ......
FROM ...
WHERE
PickupTime IS NOT NULL
ELSE IF @ReturnTimedOrNotTimed = 'N'
SELECT ......
FROM ...
WHERE
PickupTime IS NULL
ELSE IF @ReturnTimedOrNotTimed = 'B'
SELECT ......
FROM ...

I'd rather avoid restating the entire query 3 times and I know I should
avoid dynamic sql where possible. I've been trying to figure out how to put
the @ReturnTimedOrNotTimed for all 3 cases in the WHERE part of one query
but what I tried slows it down a lot (3 times slower).

SELECT ......
FROM ...
WHERE
(@ReturnTimedOrNotTimed = 'N' AND PickupTime IS NULL)
OR
(@ReturnTimedOrNotTimed = 'T' AND PickupTime IS NOT NULL)
OR
(@ReturnTimedOrNotTimed = 'B') -- "B" is for "Both"

I get the result set I expect for each value of @ReturnTimedOrNotTimed but
again, it's much slower than the first way. Is there a good way to write a
where clause that depends on an input parameter like this? I tried a CASE
construct in the WHERE but couldn't get it to even compile. Not sure if
that's possible.

SELECT ......
FROM ...
WHERE
CASE @ReturnTimedOrNotTimed
WHEN 'N' THEN PickupTime IS NULL
WHEN 'U' THEN PickupTime IS NOT NULL
END

TIA,

Keith


Re: help with conditions in WHERE clause Erland Sommarskog
1/1/2007 10:22:08 AM
Keith G Hicks (krh@comcast.net) writes:
[quoted text, click to view]

These types of searches often gets the best performance with dynamic SQL,
but if there is only one parameter I would probably not go for dynamic SQL
myself.

Here is a variation that only requires you to repeat the query twice:

SELECT ....
FROM ....
WHERE (@ReturnTimedOrNotTimed IN ('N', 'B') AND PickupTime IS NULL)
UNION
SELECT ...
FROM ...
WHERE (@ReturnTimedOrNotTimed IN ('T', 'B') AND
PickupTime IS NOT NULL)

[quoted text, click to view]

Nigel's post gave you the syntax for CASE, but I would not expect this
to perform any better. The problem is that as soon you put that open-
ended variable, the query plan must cover all possible values for it,
since the value of the variable is not known to the optimizer when the
plan is built. (Parameter sniffing? Yes, the value can be used as
guidance, but the optimizer must build a plan which still produces the
correct result if the parameter is changed along the way.)


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: help with conditions in WHERE clause Keith G Hicks
1/1/2007 10:39:52 AM
Thanks Nigel. That makes sense. :-)

Keith

[quoted text, click to view]
The case statement returns a value which you need to test
e.g.

WHERE
1 = CASE @ReturnTimedOrNotTimed
WHEN 'N' THEN case when PickupTime IS NULL then 1 else 0 end
WHEN 'U' THEN case when PickupTime IS NOT NULL then 1 else 0
end
END


Re: help with conditions in WHERE clause Keith G Hicks
1/1/2007 10:45:30 AM
Oh, good idea. Sometimes I get so burried in one way of doign things, I
forget to step back and look for another approach. :-)

[quoted text, click to view]

These types of searches often gets the best performance with dynamic SQL,
but if there is only one parameter I would probably not go for dynamic SQL
myself.

Here is a variation that only requires you to repeat the query twice:

SELECT ....
FROM ....
WHERE (@ReturnTimedOrNotTimed IN ('N', 'B') AND PickupTime IS NULL)
UNION
SELECT ...
FROM ...
WHERE (@ReturnTimedOrNotTimed IN ('T', 'B') AND
PickupTime IS NOT NULL)

[quoted text, click to view]

Nigel's post gave you the syntax for CASE, but I would not expect this
to perform any better. The problem is that as soon you put that open-
ended variable, the query plan must cover all possible values for it,
since the value of the variable is not known to the optimizer when the
plan is built. (Parameter sniffing? Yes, the value can be used as
guidance, but the optimizer must build a plan which still produces the
correct result if the parameter is changed along the way.)


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Re: help with conditions in WHERE clause Erland Sommarskog
1/1/2007 11:48:07 AM
Uri Dimant (urid@iscar.co.il) writes:
[quoted text, click to view]

UNION! Why is left as an exercise to the reader. :-)

[quoted text, click to view]

Happy New Year to you, Uri, and everyone else!

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: help with conditions in WHERE clause Erland Sommarskog
1/1/2007 11:55:15 AM
Erland Sommarskog (esquel@sommarskog.se) writes:
[quoted text, click to view]

Stupid me! I was thinking that since we have the condition
@ReturnTimedOrNotTimed = 'B' in both SELECT; we need UNION to weed out
the duplicates. But since the conditions on PickupTime are mutually
exclusive, UNION ALL is of course what we should use.

Thanks, Uri, for setting me straight!


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: help with conditions in WHERE clause Uri Dimant
1/1/2007 12:29:16 PM
Erland

[quoted text, click to view]

UNION or UNION ALL :-))))
Happy new year , and waiting for more new acrticles covering SQL Server 2005


[quoted text, click to view]

Re: help with conditions in WHERE clause Steve Dassin
1/1/2007 1:27:22 PM
[quoted text, click to view]

I'm forwarding this thought to MS....and the White House.

best wishes for the new year,

AddThis Social Bookmark Button