10

我想构建一个包含以下逻辑的 CASE 语句,但 sql 编译器不喜欢我的语句中的“或”:

CASE expression
WHEN expression1 OR expression2
THEN <yadda yadda>
ELSE <yadda yadda>
END

下面更具体的代码:

CASE @var1
WHEN '99' OR '22'   
            THEN        
                (CASE @var2
                WHEN 'All' THEN col1
                ELSE @var2
                END)
END
4

2 回答 2

10
DECLARE @Variable INT;
SET @Variable = 1;

SELECT
    CASE
        WHEN @Variable = 1 OR @Variable = 2 THEN 'It is 1 or 2'
        WHEN @Variable = 3 THEN 'It is 3'
        ELSE 'It is not 1, 2, or 3'
    END AS [SomeField]

CASEORExpressions的 MSDN 文档。

于 2012-10-02T19:43:58.090 回答
6

根据您的编辑,您甚至不需要OR声明:

CASE 
    WHEN @var1 IN ('99', '22')                
    THEN                         
         (CASE @var2                 
          WHEN 'All' 
          THEN col1                 
          ELSE @var2                 
          END) 
    END 
于 2012-10-02T20:02:02.340 回答