Solved

If Then Else in WHERE Clause

Posted on 2006-11-06
3
262 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
3 Comments
 
LVL 142

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Backup skipping a few tables 7 29
INSERT INTO SELECT JOIN THING 2 27
sql calculate averages 18 32
Merge Statement 3 9
In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now