Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
madtowner11
Starting Member
1 Post |
Posted - 2006-09-13 : 12:31:10
|
Greetings! This is my first ever post here, so please be gentle.I have a stored procedure that will be accepting many parameters (around 10). Any number of them can be empty (empty with a length of zero, but probably not Null per se).How do I do a Select... Where... where if the parameter is empty it ignores it in the 'Where' but if the parameter had anything in it, it becomes part of the filter?Thanks! |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-13 : 13:18:58
|
OrSELECT * FROM MyTableWHERE (ISNULL(@Param1, '') = '' OR Column1 = @Param1)AND (ISNULL(@Param2, '') = '' OR Column2 = @Param2)AND (ISNULL(@Param3, '') = '' OR Column3 = @Param3)AND (ISNULL(@Param4, '') = '' OR Column4 = @Param4)AND (ISNULL(@Param5, '') = '' OR Column5 = @Param5)AND (ISNULL(@Param6, '') = '' OR Column6 = @Param6)AND (ISNULL(@Param7, '') = '' OR Column7 = @Param7)AND (ISNULL(@Param8, '') = '' OR Column8 = @Param8)AND (ISNULL(@Param9, '') = '' OR Column9 = @Param9)AND (ISNULL(@Param10, '') = '' OR Column10 = @Param10)Peter LarssonHelsingborg, Sweden |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-14 : 05:52:06
|
quote: Originally posted by Peso OrSELECT * FROM MyTableWHERE (ISNULL(@Param1, '') = '' OR Column1 = @Param1)AND (ISNULL(@Param2, '') = '' OR Column2 = @Param2)AND (ISNULL(@Param3, '') = '' OR Column3 = @Param3)AND (ISNULL(@Param4, '') = '' OR Column4 = @Param4)AND (ISNULL(@Param5, '') = '' OR Column5 = @Param5)AND (ISNULL(@Param6, '') = '' OR Column6 = @Param6)AND (ISNULL(@Param7, '') = '' OR Column7 = @Param7)AND (ISNULL(@Param8, '') = '' OR Column8 = @Param8)AND (ISNULL(@Param9, '') = '' OR Column9 = @Param9)AND (ISNULL(@Param10, '') = '' OR Column10 = @Param10)Peter LarssonHelsingborg, Sweden
or this...SELECT * FROM MyTableWHERE (@Param1 IS NULL OR Column1 = @Param1)AND (@Param2 Is NULL OR Column2 = @Param2).... to avoid TABLE SCAN due to non-SARGable conditionHarsh AthalyeIndia."Nothing is Impossible" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-14 : 06:17:31
|
Then you don't catch thisquote: Any number of them can be empty (empty with a length of zero, but probably not Null per se).
Peter LarssonHelsingborg, Sweden |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-14 : 07:27:31
|
quote: Originally posted by Peso Then you don't catch thisquote: Any number of them can be empty (empty with a length of zero, but probably not Null per se).
Peter LarssonHelsingborg, Sweden
oh, right !!Thanks peter!Harsh AthalyeIndia."Nothing is Impossible" |
|
|
|
|
|
|
|