| Author |
Topic |
|
akashenk
Posting Yak Master
111 Posts |
Posted - 2006-07-26 : 16:15:30
|
| It seems that whenever I have a situation where a parameter is passed to an SP, and that parameter can have a valid null value, that my procedure becomes quite lengthy with lots of IF...ELSE structure. As a simple example,the following code takes a nullable input parameter @HometownIdIF NOT IS NULL @Hometown BEGIN SELECT * FROM MyTable WHERE HomeTown=@HomeTownId ENDELSE BEGIN |
|
|
akashenk
Posting Yak Master
111 Posts |
Posted - 2006-07-26 : 16:18:41
|
| Sorry... posted by accident (darn default buttons).... Any ways, the full code is...IF @Hometown IS NOT NULL BEGIN SELECT * FROM MyTable WHERE HomeTown=@HomeTownId ENDELSE BEGIN SELECT * FROM MyTable WHERE HomeTown IS NULL ENDThis obviously leades to longer and more complex code. Does anyone know of a way to achieve the same result in amore cocise way? Thanks. |
 |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2006-07-26 : 16:25:20
|
| a CASE statement maybe?-ec |
 |
|
|
akashenk
Posting Yak Master
111 Posts |
Posted - 2006-07-26 : 16:29:10
|
| Well, CASE or IF..ELSE, it still leads to the more complex code. I was hoping there was some sort of Built in Function in SQL Server. The problem is that [tablefield]=NULL is not the same as [tablefield] IS NULL. I suppose the developers of T-SQL had good reason for this, but it sure causes a few minor problems. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-07-27 : 11:54:11
|
| TrySELECT * FROM MyTable WHERE (HomeTown=@HomeTownId or @HomeTownId is null)MadhivananFailing to plan is Planning to fail |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-07-27 : 12:58:05
|
quote: Originally posted by madhivanan TrySELECT * FROM MyTable WHERE (HomeTown=@HomeTownId or @HomeTownId is null)MadhivananFailing to plan is Planning to fail
If @hometowndid is null then this will return that entire table !Will this work?:where isNull(hometownid,-69) = isNull(@hometownid,-69)Be One with the OptimizerTG |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-27 : 14:18:06
|
Or this?...SELECT * FROM MyTablewhere (HomeTown = @HomeTown or (@HomeTown is null and HomeTown is null)) Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-07-28 : 12:13:28
|
| Thanks for the correction TGMadhivananFailing to plan is Planning to fail |
 |
|
|
akashenk
Posting Yak Master
111 Posts |
Posted - 2006-07-28 : 12:33:44
|
| Thanks for all your suggestions. I went withSELECT * FROM MyTablewhere (HomeTown = @HomeTown or (@HomeTown is null and HomeTown is null))which appears to work and is certainly more concise than all those IF...THEN trees! |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-07-29 : 05:01:44
|
| I hate changing the setting for ANSI_NULLS, but it might offer a solution - i.e. HomeTown = @HomeTown would be TRUE if both were NULLKristen |
 |
|
|
akashenk
Posting Yak Master
111 Posts |
Posted - 2006-07-30 : 09:37:20
|
| That is also an interesting solution. What are the drabacks to turning ansi nulls off. I thought it only made it so you had to explicitly define a nullable field as such. |
 |
|
|
blindman
Master Smack Fu Yak Hacker
2365 Posts |
Posted - 2006-07-30 : 10:02:13
|
| SELECT * FROM MyTablewhere HomeTown = @HomeTown or coalesce(@HomeTown, HomeTown) is null |
 |
|
|
akashenk
Posting Yak Master
111 Posts |
Posted - 2006-07-30 : 10:12:34
|
| it seems to me that the coalesce function will give the same results as (@HomeTown is null and HomeTown is null). While the code is slightly more concise, wouldn't the performance be slightly worse due to the need to call an intrinsic function? |
 |
|
|
PSamsig
Constraint Violating Yak Guru
384 Posts |
Posted - 2006-07-30 : 20:55:59
|
| With mine playing around with coalesce() it seems that it gets translated into an implicit CASE (the optimizer calls it IF/ELSE though) construct and not a function call, so I doubt it affects performance.-- This one's tricky. You have to use calculus and imaginary numbers for this. You know, eleventeen, thirty-twelve and all those. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-07-31 : 04:48:56
|
WHERE COALESCE(NullIf(HomeTown, @HomeTown), @HomeTown, HomeTown) IS NULLQualifies as, what we call here, "Write only code" I think you can skip the final [HomeTown] parameter, but I haven't thought about it too deeply.Kristen |
 |
|
|
|