Solved

If Then Else in WHERE Clause

Posted on 2006-11-06
3
293 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Database maintenance 36 99
How do I figure out how much database is expected to grow? 15 73
SSIS GUID Variable 2 30
Convert time stamp to date 2 55
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

740 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