SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 empty parameters into a Stored Procedure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

madtowner11
Starting Member

1 Posts

Posted - 09/13/2006 :  12:31:10  Show Profile  Reply with Quote
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

USA
37460 Posts

Posted - 09/13/2006 :  12:41:11  Show Profile  Visit tkizer's Homepage  Reply with Quote
Switch your application to send in NULL values for the paramters that will be empty, then use COALESCE:

http://www.sqlteam.com/item.asp?ItemID=2077

Tara Kizer
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 09/13/2006 :  13:18:58  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Or

SELECT * FROM MyTable
WHERE (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 Larsson
Helsingborg, Sweden
Go to Top of Page

harsh_athalye
Flowing Fount of Yak Knowledge

India
5581 Posts

Posted - 09/14/2006 :  05:52:06  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
quote:
Originally posted by Peso

Or

SELECT * FROM MyTable
WHERE (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 Larsson
Helsingborg, Sweden



or this...

SELECT * FROM MyTable
WHERE (@Param1 IS NULL OR Column1 = @Param1)
AND (@Param2 Is NULL OR Column2 = @Param2)
....


to avoid TABLE SCAN due to non-SARGable condition


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30282 Posts

Posted - 09/14/2006 :  06:17:31  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Then you don't catch this
quote:
Any number of them can be empty (empty with a length of zero, but probably not Null per se).



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

harsh_athalye
Flowing Fount of Yak Knowledge

India
5581 Posts

Posted - 09/14/2006 :  07:27:31  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
quote:
Originally posted by Peso

Then you don't catch this
quote:
Any number of them can be empty (empty with a length of zero, but probably not Null per se).



Peter Larsson
Helsingborg, Sweden



oh, right !!

Thanks peter!

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000