4

我正在编写一个带有 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 列和更多的连接和参数,而不是这里定义的。我只是试图在这里让它变得不那么复杂。

    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 
4

3 回答 3

6

使用括号:

 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)))
于 2012-06-19T16:56:19.003 回答
3

你可以通过两种方式做到这一点。一种方法是使用动态 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 子句可能无法利用适用的索引。

于 2012-06-19T16:56:33.200 回答
-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

这很复杂,但只有这种方法是可能的

于 2012-06-19T17:00:00.327 回答