SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help in creating SQL Query for Store Procedure


Help in creating SQL Query for Store Procedure

Author
Message
Hogie503
Hogie503
SSC-Enthusiastic
SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)SSC-Enthusiastic (121 reputation)

Group: General Forum Members
Points: 121 Visits: 81

Hello 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



drew.allen
drew.allen
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62280 Visits: 16614
Hogie503 - Wednesday, February 28, 2018 12:31 PM

Hello 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
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Bhushan Kulkarni
Bhushan Kulkarni
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2593 Visits: 586
You can use dynamic SQL inside the stored procedure.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search