473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Allowing a Stored Procedure Argument to be NULL

I've written a stored procedure and would like to filter the results
returned based on the argument. It works so far, but I'd also like to allow
this argument to be null.

I know I can rewrite my procedure using ISNULL(). But then it won't work for
the non-null arguments.

Does anyone know a trick to make this work? Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jun 27 '08 #1
9 1260
Search google and you should find plenty of articles discussing various
approaches to this requirement. Here's a sample Where clause that works for
both null and non-null arguments:

Where (@city Is Null Or @city = Address.city)
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eb**************@TK2MSFTNGP03.phx.gbl...
I've written a stored procedure and would like to filter the results
returned based on the argument. It works so far, but I'd also like to
allow this argument to be null.

I know I can rewrite my procedure using ISNULL(). But then it won't work
for the non-null arguments.

Does anyone know a trick to make this work? Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jun 27 '08 #2
Paul,
Search google and you should find plenty of articles discussing various
approaches to this requirement. Here's a sample Where clause that works
for both null and non-null arguments:
Yeah, I searched a bit but wasn't coming up with anything useful so far.
Where (@city Is Null Or @city = Address.city)
Unfortunately, that won't cut it. If @City is not NULL, then records with
City IS NULL should not be returned. It's like I want to say WHERE
City=@City, but, if @City is NULL, then it doesn't work like I'd expect it
to.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
>
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eb**************@TK2MSFTNGP03.phx.gbl...
>I've written a stored procedure and would like to filter the results
returned based on the argument. It works so far, but I'd also like to
allow this argument to be null.

I know I can rewrite my procedure using ISNULL(). But then it won't work
for the non-null arguments.

Does anyone know a trick to make this work? Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jun 27 '08 #3
thats because in set theory the empty set (null) does not equal another
empty set. you can use the coalesce function:
where coalesce(@city,address.city,'') = coalesce(address.city,'')

-- bruce (sqlwork.com)
Jonathan Wood wrote:
Paul,
>Search google and you should find plenty of articles discussing
various approaches to this requirement. Here's a sample Where clause
that works for both null and non-null arguments:

Yeah, I searched a bit but wasn't coming up with anything useful so far.
>Where (@city Is Null Or @city = Address.city)

Unfortunately, that won't cut it. If @City is not NULL, then records
with City IS NULL should not be returned. It's like I want to say WHERE
City=@City, but, if @City is NULL, then it doesn't work like I'd expect
it to.

Thanks.
Jun 27 '08 #4
just take it a step further then

WHERE @City is null OR ((@City is not null) AND (@City = Address.city))

That way it's either null, or it's not null and it matches the city field.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:e9**************@TK2MSFTNGP02.phx.gbl...
Paul,
>Search google and you should find plenty of articles discussing various
approaches to this requirement. Here's a sample Where clause that works
for both null and non-null arguments:

Yeah, I searched a bit but wasn't coming up with anything useful so far.
>Where (@city Is Null Or @city = Address.city)

Unfortunately, that won't cut it. If @City is not NULL, then records with
City IS NULL should not be returned. It's like I want to say WHERE
City=@City, but, if @City is NULL, then it doesn't work like I'd expect it
to.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
>>
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eb**************@TK2MSFTNGP03.phx.gbl...
>>I've written a stored procedure and would like to filter the results
returned based on the argument. It works so far, but I'd also like to
allow this argument to be null.

I know I can rewrite my procedure using ISNULL(). But then it won't work
for the non-null arguments.

Does anyone know a trick to make this work? Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jun 27 '08 #5
If @city is not null, then a null Address.city will not satisfy either part
of the Where clause. @city=Address.city will be false if Address.city is
null. Try it and you'll see it works. I guess I should add that I'm using
SQL Server 2005 and it works correctly there. I know it has worked in
earlier SQL Server versions, and should work in any db, but I haven't tested
it elsewhere.

Bruce suggested Where coalesce(@city, address.city, "") =
coalesce(address.city, '') which would also work. The disadvantage to this
format is it cannot use an index on Address.city if one exists, since both
sides of the expression are functions. Even without an index I would expect
the first form to run a little faster since it doesn't have to evaluate any
functions.

"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:e9**************@TK2MSFTNGP02.phx.gbl...
Paul,
>Search google and you should find plenty of articles discussing various
approaches to this requirement. Here's a sample Where clause that works
for both null and non-null arguments:

Yeah, I searched a bit but wasn't coming up with anything useful so far.
>Where (@city Is Null Or @city = Address.city)

Unfortunately, that won't cut it. If @City is not NULL, then records with
City IS NULL should not be returned. It's like I want to say WHERE
City=@City, but, if @City is NULL, then it doesn't work like I'd expect it
to.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
>>
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eb**************@TK2MSFTNGP03.phx.gbl...
>>I've written a stored procedure and would like to filter the results
returned based on the argument. It works so far, but I'd also like to
allow this argument to be null.

I know I can rewrite my procedure using ISNULL(). But then it won't work
for the non-null arguments.

Does anyone know a trick to make this work? Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jun 27 '08 #6
You can branch the logic.

IF (@city IS NULL)
BEGIN
--NULL version
END
ELSE
BEGIN
--Non NULL version
END

You will end up with duplicate code in most instances, but it works.

If your logic is very complex, you can use branches to set up records in a
temp table that fit your desires. You start with one that gives the
narrowest set of IDs and then prune out numbers with other branches. Then
actual query ends up something like:

SELECT * FROM Table1
WHERE ID IN (SELECT ID FROM #Temp)

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eb**************@TK2MSFTNGP03.phx.gbl...
I've written a stored procedure and would like to filter the results
returned based on the argument. It works so far, but I'd also like to
allow this argument to be null.

I know I can rewrite my procedure using ISNULL(). But then it won't work
for the non-null arguments.

Does anyone know a trick to make this work? Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jun 27 '08 #7
"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:eZ**************@TK2MSFTNGP05.phx.gbl...
just take it a step further then

WHERE @City is null OR ((@City is not null) AND (@City = Address.city))

That way it's either null, or it's not null and it matches the city field.
Bruce has provided the correct method for this i.e. to use the COALESCE
function.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #8
bruce,
thats because in set theory the empty set (null) does not equal another
empty set. you can use the coalesce function:
Yeah, I kind of figured that out. Just trying to find an easy workaround.
where coalesce(@city,address.city,'') = coalesce(address.city,'')
I'm just trying to understand this. Can you explain why it wouldn't just be:

WHERE COALESCE(@city,'') = coalesce(address.city,'')

To the extent I understand it (not that much), it seems like your version
might produce a false match if @city is null, but address.city is not.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Jun 27 '08 #9
Well, now that's starting to get a little convoluted. But, unless I'm
missing something, it would not detect the case where @City is NULL and
Address.City is not NULL. In this case, there should be no match. It would
probably need to be even a bit more convoluted.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:eZ**************@TK2MSFTNGP05.phx.gbl...
just take it a step further then

WHERE @City is null OR ((@City is not null) AND (@City = Address.city))

That way it's either null, or it's not null and it matches the city field.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:e9**************@TK2MSFTNGP02.phx.gbl...
>Paul,
>>Search google and you should find plenty of articles discussing various
approaches to this requirement. Here's a sample Where clause that works
for both null and non-null arguments:

Yeah, I searched a bit but wasn't coming up with anything useful so far.
>>Where (@city Is Null Or @city = Address.city)

Unfortunately, that won't cut it. If @City is not NULL, then records with
City IS NULL should not be returned. It's like I want to say WHERE
City=@City, but, if @City is NULL, then it doesn't work like I'd expect
it to.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
>>>
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eb**************@TK2MSFTNGP03.phx.gbl...
I've written a stored procedure and would like to filter the results
returned based on the argument. It works so far, but I'd also like to
allow this argument to be null.

I know I can rewrite my procedure using ISNULL(). But then it won't
work for the non-null arguments.

Does anyone know a trick to make this work? Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brian | last post by:
I am trying to pass a null value into a stored procedure so that it can save the data. I am using Microsoft's SQLHelper dll to do this. My example code is below. How do I pass in a null value...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
2
by: Dave Anderson | last post by:
I really like Bob's "stored procedure as method of Connection Object" technique. It is convenient, compact, and concise, and simplifies protection from SQL injection. HOWEVER, I cannot figure...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
4
by: hicks | last post by:
I'm trying to invoke a DB2 stored procedure. The stored proc is coded in C and compiled to a shared library, which has been placed in the <DB2 dir>/functions directory. The platform is Solaris....
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
5
by: wpellett | last post by:
I can not get the SQL compiler to rewrite my SQL UPDATE statement to include columns being SET in a Stored Procedure being called from a BEFORE UPDATE trigger. Example: create table...
9
by: fniles | last post by:
I am using VB.NET 2003 and SQL2000 database. I have a stored procedure called "INSERT_INTO_MYTABLE" that accepts 1 parameter (varchar(10)) and returns the identity column value from that table....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.