Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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)
 Query Help for paramters when null?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

akpaga
Constraint Violating Yak Guru

USA
331 Posts

Posted - 10/21/2013 :  16:17:27  Show Profile  Reply with Quote
Hi friends,

I have a query in the following way

select * from Customer
where CustmerNumber=@CustomerNumber
and Date_Customer=@date_Customer

But the problem i have is to return all records when the parameters are null and return records even if one parameter is passed like only customerNumber is defined but not Date_customer and vice versa...

How can i achiove this...Thank you

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 10/21/2013 :  16:31:40  Show Profile  Reply with Quote
The term for that sort of query is a catch-all query. There are several ways to write them. Here is one:
select * from Customer
where 
	(CustmerNumber=@CustomerNumber or @CustomerNumber IS NULL)
	and (Date_Customer=@date_Customer or @date_Customer IS NULL)
Here is a link that gives more detail on these types of queries:
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
Go to Top of Page

akpaga
Constraint Violating Yak Guru

USA
331 Posts

Posted - 10/21/2013 :  17:12:30  Show Profile  Reply with Quote
thank you lamprey..works good.
Go to Top of Page
  Previous Topic Topic Next 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.06 seconds. Powered By: Snitz Forums 2000