Solved

If Then Else in WHERE Clause

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
This query failed in sql 2014 5 29
insert wont work in SQL 14 19
Sql server, import complete table, using vb.net 9 33
SQL view 2 26
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

786 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