SQL案例表达式语法?

时间:2008-08-07 12:13:01

标签: sql sql-server oracle syntax case

SQL Case表达式的完整和正确语法是什么?

9 个答案:

答案 0 :(得分:77)

完整语法取决于您正在使用的数据库引擎:

对于SQL Server:

CASE case-expression
    WHEN when-expression-1 THEN value-1
  [ WHEN when-expression-n THEN value-n ... ]
  [ ELSE else-value ]
END

或:

CASE
    WHEN boolean-when-expression-1 THEN value-1
  [ WHEN boolean-when-expression-n THEN value-n ... ]
  [ ELSE else-value ]
END

表达式等:

case-expression    - something that produces a value
when-expression-x  - something that is compared against the case-expression
value-1            - the result of the CASE statement if:
                         the when-expression == case-expression
                      OR the boolean-when-expression == TRUE
boolean-when-exp.. - something that produces a TRUE/FALSE answer

链接:CASE (Transact-SQL)

另请注意,WHEN语句的顺序很重要。您可以轻松编写多个重叠的WHEN子句,并使用匹配的第一个

注意:如果未指定ELSE子句,且未找到匹配的WHEN条件,则CASE表达式的值将为 NULL

答案 1 :(得分:21)

考虑到您标记了多个产品,我会说完整正确的语法将是ISO / ANSI SQL-92标准中的语法:

<case expression> ::=
       <case abbreviation>
     | <case specification>

<case abbreviation> ::=
       NULLIF <left paren> <value expression> <comma>
              <value expression> <right paren>
     | COALESCE <left paren> <value expression>
                { <comma> <value expression> }... <right paren>

<case specification> ::=
       <simple case>
     | <searched case>

<simple case> ::=
     CASE <case operand>
          <simple when clause>...
        [ <else clause> ]
     END

<searched case> ::=
     CASE
       <searched when clause>...
     [ <else clause> ]
     END

<simple when clause> ::= WHEN <when operand> THEN <result>

<searched when clause> ::= WHEN <search condition> THEN <result>

<else clause> ::= ELSE <result>

<case operand> ::= <value expression>

<when operand> ::= <value expression>

<result> ::= <result expression> | NULL

<result expression> ::= <value expression>

语法规则

1) NULLIF (V1, V2) is equivalent to the following <case specification>:

     CASE WHEN V1=V2 THEN NULL ELSE V1 END

2) COALESCE (V1, V2) is equivalent to the following <case specification>:

     CASE WHEN V1 IS NOT NULL THEN V1 ELSE V2 END

3) COALESCE (V1, V2, . . . ,n ), for n >= 3, is equivalent to the
   following <case specification>:

     CASE WHEN V1 IS NOT NULL THEN V1 ELSE COALESCE (V2, . . . ,n )
     END

4) If a <case specification> specifies a <simple case>, then let CO
   be the <case operand>:

   a) The data type of each <when operand> WO shall be comparable
      with the data type of the <case operand>.

   b) The <case specification> is equivalent to a <searched case>
      in which each <searched when clause> specifies a <search
      condition> of the form "CO=WO".

5) At least one <result> in a <case specification> shall specify a
   <result expression>.

6) If an <else clause> is not specified, then ELSE NULL is im-
   plicit.

7) The data type of a <case specification> is determined by ap-
   plying Subclause 9.3, "Set operation result data types", to the
   data types of all <result expression>s in the <case specifica-
   tion>.

Access Rules

   None.

General Rules

1) Case:

   a) If a <result> specifies NULL, then its value is the null
      value.

   b) If a <result> specifies a <value expression>, then its value
      is the value of that <value expression>.

2) Case:

   a) If the <search condition> of some <searched when clause> in
      a <case specification> is true, then the value of the <case
      specification> is the value of the <result> of the first
      (leftmost) <searched when clause> whose <search condition> is
      true, cast as the data type of the <case specification>.

   b) If no <search condition> in a <case specification> is true,
      then the value of the <case expression> is the value of the
      <result> of the explicit or implicit <else clause>, cast as
      the data type of the <case specification>.

答案 2 :(得分:19)

以下是CASE statement examples from the PostgreSQL docs(Postgres遵循SQL标准):

SELECT a,
   CASE WHEN a=1 THEN 'one'
        WHEN a=2 THEN 'two'
        ELSE 'other'
   END
FROM test;

SELECT a,
   CASE a WHEN 1 THEN 'one'
          WHEN 2 THEN 'two'
          ELSE 'other'
   END
FROM test;

显然,当您只是根据可能值列表检查一个字段时,第二种形式更清晰。第一种形式允许更复杂的表达。

答案 3 :(得分:8)

Sybase与SQL Server具有相同的case syntax

描述

支持条件SQL表达式;可以在任何可以使用值表达式的地方使用。

语法

case 
     when search_condition then expression 
    [when search_condition then expression]...
    [else expression]
end

案例和值语法

case expression
     when expression then expression 
    [when expression then expression]...
    [else expression]
end

参数

情况下

开始案例表达。

在搜索条件或要比较的表达式之前。

search_condition

用于设置所选结果的条件。案例表达式的搜索条件类似于where子句中的搜索条件。 “Transact-SQL用户指南”中详细介绍了搜索条件。

然后

在指定case的结果值的表达式之前。

表达

是列名,常量,函数,子查询,或由算术或按位运算符连接的列名,常量和函数的任意组合。有关表达式的更多信息,请参阅。

中的“表达式”

实施例

select disaster, 
       case
            when disaster = "earthquake" 
                then "stand in doorway"
            when disaster = "nuclear apocalypse" 
                then "hide in basement"
            when monster = "zombie apocalypse" 
                then "hide with Chuck Norris"
            else
                then "ask mom"
       end 
  from endoftheworld

答案 4 :(得分:4)

我挖出了相同的Oracle页面,看起来这是相同的语法,只是略有不同。

链接:Oracle/PLSQL: Case Statement

答案 5 :(得分:3)

Oracle syntax from the 11g Documentation

CASE { simple_case_expression | searched_case_expression }
     [ else_clause ]
     END

simple_case_expression

expr { WHEN comparison_expr THEN return_expr }...

searched_case_expression

{ WHEN condition THEN return_expr }...

else_clause

ELSE else_expr

答案 6 :(得分:1)

在Oracle的情况下需要注意的一点是,如果匹配时没有,并且没有其他部分引发异常。

答案 7 :(得分:-1)

在这里您可以找到MySQL case statements in SQL的完整指南。

CASE
     WHEN some_condition THEN return_some_value
     ELSE return_some_other_value
END

答案 8 :(得分:-2)

SQL SERVER中的case语句语法:

CASE column
   WHEN value1 THEN 1
   WHEN value3 THEN 2
   WHEN value3 THEN 3
   WHEN value1 THEN 4
   ELSE ''
END

我们也可以使用如下:

CASE 
   WHEN column=value1 THEN 1
   WHEN column=value3 THEN 2
   WHEN column=value3 THEN 3
   WHEN column=value1 THEN 4
   ELSE ''
END