sarsha
sarsha

Reputation: 73

To use If in where clause in a stored procedure

I am writing a stored procedure with 3 parameters and my where clause changes depending on one of these parameteres. Is it possible to write a SQL query in this way -

    CREATE PROCEDURE [dbo].[VendorVettingModal] @column NVarchar (50), @applicanttype NVarchar (10), @donotuse int AS

                declare @column NVarchar (50), @applicanttype NVarchar (10), @donotuse int

    select a.Id, a.Firstname, rs.Status,cs.ClearanceStatus
    from applicant a 
    left join ReviewStatus rs on a.ReviewStatus = rs.Id 
    left join ClearanceStatus cs on a.ClearanceStatus = cs.Id
    where
    if(@column = 'Recruiting')
    begin
        a.applicanttype = @applicanttype and a.reviewstatus = 7 and a.donotuse = @donotuse      
    end
    else if(@column = 'Clearance')
    begin
        a.applicanttype = @applicanttype and (a.reviewstatus != 7 or a.reviewstatus is null) and a.donotuse = @donotuse     
    end 

Rather than writing this way? Because I have about 20-25 columns and a lot more joins and where params than defined here. I have just tried to make it less complicated here.

    CREATE PROCEDURE [dbo].[VendorVettingModal] @column NVarchar (50), @applicanttype NVarchar (10), @donotuse int AS

                declare @column NVarchar (50), @applicanttype NVarchar (10), @donotuse int

    if(@column = 'Recruiting')
    begin
        select a.Id, a.Firstname, rs.Status,cs.ClearanceStatus
        from applicant a 
        left join ReviewStatus rs on a.ReviewStatus = rs.Id 
        left join ClearanceStatus cs on a.ClearanceStatus = cs.Id
        where
        a.applicanttype = @applicanttype and a.reviewstatus = 7 and a.donotuse = @donotuse      
    end
    else if(@column = 'Clearance')
    begin
        select a.Id, a.Firstname, rs.Status,cs.ClearanceStatus
        from applicant a 
        left join ReviewStatus rs on a.ReviewStatus = rs.Id 
        left join ClearanceStatus cs on a.ClearanceStatus = cs.Id
        where
        a.applicanttype = @applicanttype and (a.reviewstatus != 7 or a.reviewstatus is null) and a.donotuse = @donotuse     
    end 

Upvotes: 3

Views: 14237

Answers (3)

Matt Smucker
Matt Smucker

Reputation: 5244

Use parenthesis:

 select a.Id, a.Firstname, rs.Status,cs.ClearanceStatus
    from applicant a 
    left join ReviewStatus rs on a.ReviewStatus = rs.Id 
    left join ClearanceStatus cs on a.ClearanceStatus = cs.Id
    where a.applicanttype = @applicanttype
    and a.donotuse = @donotuse 
    AND ((@column = 'Recruiting' AND (a.reviewstatus = 7))
    OR
    (@column = 'Clearance' AND (a.reviewstatus != 7 or a.reviewstatus is null)))

Upvotes: 6

batressc
batressc

Reputation: 1588

Unfortunalety, in transact-sql the "if-else-if-else" only can write this: http://msdn.microsoft.com/en-us/library/ms182587.aspx

DECLARE @Number int;
SET @Number = 50;
IF @Number > 100
   PRINT 'The number is large.';
ELSE 
    BEGIN
        IF @Number < 10
            PRINT 'The number is small.';
        ELSE
            PRINT 'The number is medium.';
    END;
GO

It's complicated, but only this way is possible

Upvotes: -2

Gordon Linoff
Gordon Linoff

Reputation: 1271041

You can do this two ways. One way uses dynamic SQL. However, that is not generalizable to any database. The alternative is to structure the WHERE clause as:

where (case when @column = 'Recruiting' and
                 a.applicanttype = @applicanttype and a.reviewstatus = 7 and a.donotuse = @donotuse
            then 'True'
            when @column = 'Clearance' and
                 a.applicanttype = @applicanttype and (a.reviewstatus != 7 or a.reviewstatus is null) and a.donotuse = @donotuse
            then 'True'
            . . .
       end) = 'True'

Two advantages of this over dynamic SQL is that the query does not have to be recompiled and it works in a broader range of databases. One disadvantage is that the WHERE clause may not take advantage of applicable indexes.

Upvotes: 3

Related Questions