在存储过程的where子句中使用If

时间:2012-06-19 16:50:13

标签: sql stored-procedures

我正在编写一个包含3个参数的存储过程,而我的where子句根据其中一个参数变化。是否可以用这种方式编写SQL查询 -

    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 

而不是这样写?因为我有大约20-25列和更多的连接和params比这里定义的更多。我刚刚试图让它变得不那么复杂。

    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 

3 个答案:

答案 0 :(得分:5)

使用括号:

 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)))

答案 1 :(得分:2)

你可以这两种方式。一种方法使用动态SQL。但是,这不适用于任何数据库。另一种方法是将WHERE子句构造为:

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'

这比动态SQL的两个优点是查询不必重新编译,并且可以在更广泛的数据库中工作。一个缺点是WHERE子句可能没有利用适用的索引。

答案 2 :(得分:-2)

不幸的是,在transact-sql中,“if-else-if-else”只能写出: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

这很复杂,但只有这种方式才有可能