Solved

If Then Else in WHERE Clause

Posted on 2006-11-06
300 Views
I would like to pass a string parameter into a stored procedure that will do the following

@param1 int
@Assignment nvarchar(12)

Select blah blah blah subquery1 as blah1, subquery 2 as blah2.... (Long drawn out select query)  from Table1 WHERE
Division = @param1 AND ....

if @Assignment = 'Unassigned' then the where clause should be AssignedTo = '-1'
if @Assignment = 'Assigned' then the where clause should be AssignedTo <> '-1'
if @Assignment = 'All' then skip this part of the where clause.

Dan
0
Question by:DigitalDan3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 143

Assisted Solution

Guy Hengel [angelIII / a3] earned 167 total points
ID: 17882164
@param1 int
@Assignment nvarchar(12)

Select blah blah blah subquery1 as blah1, subquery 2 as blah2.... (Long drawn out select query)
FROM Table1
WHERE Division = @param1
AND (
( @Assignment = 'Unassigned' AND AssignedTo = '-1' )
OR ( @Assignment = 'Assigned' AND AssignedTo <> '-1' )
OR ( @Assignment = 'All' )
)

0

LVL 75

Assisted Solution

Aneesh Retnakaran earned 167 total points
ID: 17882177
Select blah blah blah subquery1 as blah1, subquery 2 as blah2.... (Long drawn out select query)  from Table1 WHERE
Division = @param1 AND (
(@Assignment = 'Unassigned' AND AssignedTo = '-1' )
OR
(@Assignment = 'Assigned'  AND AssignedTo <> '-1')
OR
( @Assignment = 'All'  AND 1 =1 )
)
0

LVL 29

Accepted Solution

Nightman earned 166 total points
ID: 17882227
While both of those posted may work (and are more convenient given the code reusability), it would execute faster to do it like this:

Create procedure MyProcedure
(
@param1 int
@Assignment nvarchar(12)
)
as
if @Assignment='Unassigned'
begin
Select blah blah blah subquery1 as blah1, subquery 2 as blah2.... (Long drawn out select query)  from Table1 WHERE Division = @param1 AND AssignedTo = '-1'
end
else if @Assignment='Assigned'
begin
Select blah blah blah subquery1 as blah1, subquery 2 as blah2.... (Long drawn out select query)  from Table1 WHERE Division = @param1 AND AssignedTo <> '-1'
end
else if @Assignment = 'All'
begin
Select blah blah blah subquery1 as blah1, subquery 2 as blah2.... (Long drawn out select query)  from Table1 WHERE Division = @param1
end

In fact, to make it even faster (and allow SQL to cache a seperate and most likely optimal query plan for each permutation) it would be even better to have a stored procedure for EACH permutation, and have your primary procedure call each one internally based on the value of assignment.

More work, but avoids recompiles and re-use of potentially inefficient query plans.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses
Course of the Month8 days, 13 hours left to enroll