THx...could you give me an ex of the if else logic.. Glen
Main Topics
Browse All TopicsHaving a hard time and I think this is easy....
I have parameters
@Test varchar
@Test1 varchar
@Test2 varchar
I have a select statment
Select test, test1, test2 from tbl_test
where -- I match the data from the parameters
@Test = Test and
@Test1 = Test1 and
@Test2 = Test2
If the user does not pass any values for the test1 parameter, then the result set should be:
All data from the Test field and the Test2 fields only.
What am I missing here...thx
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Sure
create procedure test @Test1 int = null, @Test2 int = null
as
if @Test1 is not null and @Test2 is not null
select * from tblTest where Test1 = @Test1 and Test2 = @Test2
else
if @Test1 is not null and @Test2 is null
select * from tblTest where test1 = @Test1
else
if @Test1 is null and @Test2 is not null
select * from tblTest where test2 = @Test2
else
if @Test2 is null and @Test1 is null
select * from tblTest
More params -> more copy-pasting :). Use with care then.
wait .. I have 25 parameters in the search;
Pls look at this small amount of code:
ALTER PROCEDURE [dbo].[usp_Search1]
@Program varchar (50) = Test,
@Project varchar (255) = tt,
@Center varchar (50) = ttt,
@Facility_Name varchar (255) = NULL
AS
BEGIN
SET NOCOUNT ON;
Select p.Program, p.Project, p.Center, f.Facility_Name from tbl_Program p join tbl_Facility f on p.MainId = f.MainId
where
@Program = p.Program and
@Project = p.Project and
@Center = p.Center and
@Facility_Name is not null or @Facility_Name = f.Facility_Name
end
I defaulted the Facility_Name para to NULL and then the code bombs and I receive no results; is there another way..I should be receiving all data in the Facility_Name field
OK...please look at this code: I have values for the first 3 para; none for the last 2. I should be receiving all data for the first 3. What am I missing? Thx
ALTER PROCEDURE [dbo].[usp_Search1]
@Program varchar (50) = TTT,
@Project varchar (255) = TT,
@Center varchar (50) = T,
@Facility_Name varchar (255)= NULL,
@BuildNos varchar (255) = NULL
AS
BEGIN
SET NOCOUNT ON;
Select p.Program, p.Project, p.Center, f.Facility_Name, f.Building_Nos as 'Building Numbers'
from tbl_Program p join tbl_Facility f on p.MainId = f.MainId
where
(@Program is null or p.Program = @Program)
and (@Project is null or p.Project = @Program)
and (@Center is null or p.Center = @Center)
and (@Facility_Name is null or f.Facility_Name = @Facility_Name)
and (@BuildNos is null or f.Building_Nos = @BuildNos)
end
Business Accounts
Answer for Membership
by: maradamPosted on 2009-02-05 at 12:03:22ID: 23563499
you need to assign default values for parameters and rewrite your query like this
create procedure test @Test1 int = NULL, @Test2 int = NULL, @Test3 int = NULL
as
select * from tblTest where (test1 = @Test1 or @Test1 is null)
and (test2 = @Test2 or test2 is null)
and (test3 = @Test3 or test3 is null)
This would be rather slow - in most cases optimizer will produce a scan but it should work as you expect. you can also make if else logic and omit clauses with nulls. It should work fast if you pass not nulls in params but it needs soma copy-paste'ing.