Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Passing NULL values in a WHERE clause


Passing NULL values in a WHERE clause

Author
Message
cs.asbuilts
cs.asbuilts
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 35
I thought I had this worked out but when i started testing I was getting more records than expected.

What I want to do is select records that meet the specific condition of the parameters.

--===== If the test table already exists, drop it
IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL
DROP TABLE #mytable

--===== Create the test table with
CREATE TABLE #mytable
(
id         INT,
eq_id         VARCHAR(10),
bl_id         VARCHAR(10),
fl_id         VARCHAR(10),
rm_id         VARCHAR(10)
)

--===== Insert the test data into the test table
INSERT INTO #mytable
(id, eq_id, bl_id, fl_id, rm_id)
SELECT 1, 'AAAA','BBBB','CCCC', NULL UNION ALL
SELECT 2, 'EEEE', NULL, NULL, NULL UNION ALL
SELECT 3, NULL,'BBBB', 'CCCC', NULL UNION ALL
SELECT 4, NULL, 'QQQQ',NULL, NULL


select * from #mytable

DECLARE @eq_id varchar(10), @bl_id varchar(10), @fl_id varchar(10), @rm_id varchar(10)
set @eq_id = NULL
set @bl_id = 'BBBB'
set @fl_id = 'CCCC'
set @rm_id = NULL

select * from #mytable
   WHERE (eq_id = @eq_id or @eq_id IS NULL) AND (bl_id = @bl_id or @bl_id IS NULL)
         AND (fl_id = @fl_id or @fl_id IS NULL) AND (rm_id = @rm_id or @rm_id IS NULL)



When the last select statement gets run there are two rows returned and I understand why. What I am trying to get is the rows that meet the condition for each of the columns. There is only one row that should be returned but I can't figure out how to construct the where clause.

I did get it to work as expected using "??_id = @value" and SET ANSI NULLS OFF but this is running in a function.

Any ideas or direction would be much appreciated.

Thanks,
Craig
Dave Ballantyne
Dave Ballantyne
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1818 Visits: 8370
Hi,

Try this article on Catch All Queries
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/



Clear Sky SQL
My Blog
cs.asbuilts
cs.asbuilts
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 35
Dave,

Thanks for the link. Some good stuff when thinking about dynamic SQL. I started down the dynamic path and was breaking apart the code and realized that I had an error in the WHERE clause. I get myself in more trouble with cut/paste.

I was checking if the passed parameter was NULL not the field. I should of had:

select * from #mytable
WHERE (eq_id = @eq_id or eq_id IS NULL) AND (bl_id = @bl_id or bl_id IS NULL)
   AND (fl_id = @fl_id or fl_id IS NULL) AND (rm_id = @rm_id or rm_id IS NULL)


I am pretty sure this is giving me what I need.

Sometimes just asking the question gets things going.

Thanks,
Craig
sanjeewan99
sanjeewan99
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 Visits: 399
Hello,
Go to this link efficent way to pass All, Null , multiple value
http://www.bi-rootdata.com/2012/09/efficient-way-of-using-all-as-parameter.html
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search