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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 parameter values

Author  Topic 

HenryFulmer
Posting Yak Master

110 Posts

Posted - 2012-10-04 : 16:10:45
I have a query that take 2 parameters. parameter 1 is mandatory, parameter 2 is optional.
I would like the query to execute that when the 2nd parameter is NULL it is disregarded and only the 1st parameter from the WHERE clause is used.


SELECT Column1, Column2 FROM Table1 WHERE
Column3 = @Param1 AND ...

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-04 : 16:20:13
something like


SELECT Column1, Column2 FROM Table1
WHERE
Column3 = @Param1
AND (Column4 = @Param2 OR @Param2 IS NULL)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

HenryFulmer
Posting Yak Master

110 Posts

Posted - 2012-10-04 : 17:20:29
Then I would get nothing in my result set if @Param2 is NULL for Column4.
Every Record has a value in Column 4. My intend is to retrieve all records that match @Param1 for Column3 regardless of the Column4 value when @Param2 is NULL>
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2012-10-04 : 18:01:45
Have you tried the code?

=================================================
We are far more concerned about the desecration of the flag than we are about the desecration of our land. -Wendell Berry
Go to Top of Page

HenryFulmer
Posting Yak Master

110 Posts

Posted - 2012-10-04 : 18:08:05
Yes, I have.
When @Param2 has a value the results are as expected. When @Param2 is NULL, no results are returned. Also as expected because I have no records where Column4 is NULL.
What I would like to retrieve is everything that matches Column3.
Go to Top of Page
   

- Advertisement -