简体   繁体   中英

T-SQL SYNTAX ISSUE - using OR in CASE statement

i would like to build a CASE statement that incorporates the following logic, but the sql compiler does not like the 'OR' in my statement:

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

more specific code below:

CASE @var1
WHEN '99' OR '22'   
            THEN        
                (CASE @var2
                WHEN 'All' THEN col1
                ELSE @var2
                END)
END
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]

MSDN docs for CASE , OR , and Expressions .

Based on your edits, you don't even need an OR statement:

CASE 
    WHEN @var1 IN ('99', '22')                
    THEN                         
         (CASE @var2                 
          WHEN 'All' 
          THEN col1                 
          ELSE @var2                 
          END) 
    END 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM