Solved

If Then Else in WHERE Clause

Posted on 2006-11-06
3
300 Views
Last Modified: 2008-02-01
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.

Thanks in advance.

Dan
0
Comment
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
  • Learn & ask questions
3 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
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

by:Aneesh Retnakaran
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

by:
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question