|
Why I am getting error in using Case statement in Where clause? Am I missing something? This is not an actual query, that is re-format the actual table and column name. Thanks.
(comments are locked)
|
|
What you do is... (bits left out for you to do as your homework...) What I have done here is translate the CASE statement into a series of OR statements. However, you need to be careful with your bracketing of clauses when you do this, to ensure you get the right conditions going together. Check MS's documentation on the precedence of operators. @Tom, that was what I done in my code but it seems a bit strange to look at using OR if you add too many condition between OR. I want to change my code to CASE if that is possible. you might say .... why I want to change it, code is still working okay with OR. :-)
Dec 10 '10 at 08:01 AM
Leo
It is possible, but it's not recommended. Why not? Performance. CASE evaluates individual items, which Bad. SQL Server works best on sets of data. If you must, try reading Jeff Smith's blog post on the subject at http://weblogs.sqlteam.com/jeffs/archive/2003/11/14/513.aspx You also might want to re-read MS's documentation of the CASE clause - http://msdn.microsoft.com/en-us/library/ms181765.aspx - because it explains more about how it can and can't be used. Oh, and don't call me Tom.
Dec 10 '10 at 09:38 AM
ThomasRushton ♦
(comments are locked)
|
|
I think that this is what you're trying to do. The problem with it will be that it will be dead slow for a large table but it may be your only way of doing it for all I know! I haven't been able to test it. I believe Celko came out with a slightly cleverer version of this a while back but I can't find it. @Phil Factor This is a very good solution! Without this trick in the where clause, one would have to either use @ThomasRushton's approach or outsource the CASE activity to CTE (or sub-select) which will make the where clause simpler, but at the very high cost of needlessly pre-including potentially unwanted records and then filtering them out: -- don't do it, this is just to reveal the
-- superiority of Phil Factor's solution
with records (EmpName, Address1, IncludeIt) as
(
SELECT
T1.EmpName, T2.Address1,
CASE
WHEN 'R1' AND T1.Sex 'M'
AND T2.Address1 = 'UK' THEN 1
WHEN @Dept='R2' AND T1.Sex 'F'
and T2.Address1 = 'US' THEN 1
WHEN @Dept='R3' AND T1.Sex = ''
AND T2.Address1 = 'JAP' THEN 1
ELSE 0
END AS IncludeIt
FROM TABLE1 AS T1 INNER JOIN TABLE2 AS T2
ON T1.EmpNo = T2.EmpNo
WHERE T1.EmpName LIKE @EmpFName + '%'
)
SELECT EmpName, Address1
FROM records
WHERE IncludeIt = 1;
Dec 10 '10 at 11:05 AM
Oleg
@thomasRuston Blooming brackets. Thanks for spotting it. I removed it.
Dec 10 '10 at 11:48 AM
Phil Factor
@Phil Factor - No worries. I've now removed the comment mentioning it.
Dec 10 '10 at 01:23 PM
ThomasRushton ♦
That's fantastic! Thank you Phil.
Dec 10 '10 at 01:55 PM
Mark
(comments are locked)
|


how can you do that to make it proper sql format? I tried more then 5 times chaning the format before I post it. I can't managed to get it right. Changed whole format when I past into the box. ...
Indent code lines by four spaces. It even works in comments:
See?
There are other formatting bits that work as well, but I can't remember them all. Shouldn't that be part of the FAQs?
nice.thanks.
You can't dynamically build a where clause using a case statement.