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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 parameter values
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

HenryFulmer
Posting Yak Master

USA
110 Posts

Posted - 10/04/2012 :  16:10:45  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 10/04/2012 :  16:20:13  Show Profile  Reply with Quote
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

USA
110 Posts

Posted - 10/04/2012 :  17:20:29  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
1770 Posts

Posted - 10/04/2012 :  18:01:45  Show Profile  Reply with Quote
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

USA
110 Posts

Posted - 10/04/2012 :  18:08:05  Show Profile  Reply with Quote
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
  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