Home Forums SQL Server 2012 SQL 2012 - General Help in creating SQL Query for Store Procedure RE: Help in creating SQL Query for Store Procedure
February 28, 2018 at 12:35 pm
Hogie503 - Wednesday, February 28, 2018 12:31 PMHello SQL Experts,
I have interface with 3 TextBoxs and 3 CheckBoxes that will be passing their values as parameters to a store procedure
The selected values are always the same, [ SELECT A,B,C,D, FROM Table_1] but in the WHERE Clause I will have many options,
all combinations of Checkboxes and Textboxes@Parm1
@parm2
@parm3, .....I am trying to implement some knd of IF conditional filter in the WHERE CLAUSE,
Since the SELECT and FROM is always the same, I only want to write once and have all my options / combo filters in the
WHERE CLAUSE filter my data base on the parameter pass to the Store Procedure..Below is what I have, but with lots more parameters i will be repeating the SELECT / FROM for each filter combo
IF @par1 = ''
Select A,B,C,D
From Table_1
WHERE
Table_1.A IN (1,2,3,4,5,6,7)
--************************************************IF @par1 = 10 AND @par2 = 20
Select A,B,C,D
From Table_1
WHERE
Table_1.A IN (5,6,7,8)
AND @parm3 is like @date
--************************************************
IF @par1 = 2 AND @par2 = 3
Select A,B,C,D
From Table_1
WHERE
Table_1.A IN (10,20,30,40)
AND @par3 is like @end
--************************************************
IF @par2 != ''
Select A,B,C,D
From Table_1
WHERE
Table_1.A IN (100,200,300)I would like to have something like the below, IF possible...
Select A,B,C,D
From Table_1
WHERE
(with all the filter combination in the where clause, so I only have the SELECT / FROM one time)Not sure if I can use IF ELSE in WHERE CLAUSE or Some kind of CASE... IF so how to implement..
Any Help or suggestion will be greatly appreciated.... Thank You
This is known as a Catch-all Query. Gail does a nice job of writing this up in the link provided.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA