SQL案例表达式语法?

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

0
case expression 是正确的术语,因为它们计算为标量值(在SQL中,语句由分号结尾的“statement terminator”终止)。
额外 作者 Used_By_Already,

8 答案

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

对于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

Link: CASE (Transact-SQL)

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

Note: If no ELSE clause is specified, and no matching WHEN-condition is found, the value of the CASE expression will be NULL.

0
额外
有这一直是一个SQL的东西?我只是最近才发现这一点
额外 作者 mike james,

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

Link: Oracle/PLSQL: Case Statement

0
额外

Oracle 11g文档中的语法

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
0
额外
谢谢Rob van Wijk,你是对的。我编辑了我的答案。
额外 作者 Leigh Riffel,
这是PL / SQL案例陈述
额外 作者 Rob van Wijk,

SQL SERVER中的案例语句语法:

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
0
额外

以下是 CASE 来自PostgreSQL文档的声明示例(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;

Obviously the second f要么m is cleaner when you are just checking one field against a list of possible values. The first f要么m allows m要么e complicated expressions.

0
额外

考虑到你标记了多个产品,我会说在ISO / ANSI SQL-92标准中找到 full 正确的语法:

 ::=
       
     | 

 ::=
       NULLIF   
               
     | COALESCE  
                {   }... 

 ::=
       
     | 

 ::=
     CASE 
          ...
        [  ]
     END

 ::=
     CASE
       ...
     [  ]
     END

 ::= WHEN  THEN 

 ::= WHEN  THEN 

 ::= ELSE 

 ::= 

 ::= 

 ::=  | NULL

 ::= 

语法规则

1) NULLIF (V1, V2) is equivalent to the following :

     CASE WHEN V1=V2 THEN NULL ELSE V1 END

2) COALESCE (V1, V2) is equivalent to the following :

     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 WHEN V1 IS NOT NULL THEN V1 ELSE COALESCE (V2, . . . ,n )
     END

4) If a  specifies a , then let CO
   be the :

   a) The data type of each  WO shall be comparable
      with the data type of the .

   b) The  is equivalent to a 
      in which each  specifies a  of the form "CO=WO".

5) At least one  in a  shall specify a
   .

6) If an  is not specified, then ELSE NULL is im-
   plicit.

7) The data type of a  is determined by ap-
   plying Subclause 9.3, "Set operation result data types", to the
   data types of all s in the .

Access Rules

   None.

General Rules

1) Case:

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

   b) If a  specifies a , then its value
      is the value of that .

2) Case:

   a) If the  of some  in
      a  is true, then the value of the  is the value of the  of the first
      (leftmost)  whose  is
      true, cast as the data type of the .

   b) If no  in a  is true,
      then the value of the  is the value of the
       of the explicit or implicit , cast as
      the data type of the .
0
额外
唯一试图解决通用语法要求的人!
额外 作者 Peter Majeed,

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

0
额外
我不确定这个版本适用于什么版本。以下两者都不会在10g或11g中引发例外。选择1 = 2时的情况,然后'a'从双重结束; 2时选择1个案例然后'a'从双重结束;
额外 作者 Leigh Riffel,
这可能适用于PL / SQL中的CASE 语句,但不适用于SQL中的CASE 表达式
额外 作者 a_horse_with_no_name,
回想起来,我认为当时我正在谈论9i。 :)
额外 作者 Tanveer Badar,

Sybase具有相同的大小写语法作为SQL Server:

描述

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

句法

案件 
     什么时候 search_condition 然后 表达 
    [什么时候 search_condition 然后 表达]...
    [else 表达]
end

案件 and values 句法

案件 表达
     什么时候 表达 然后 表达 
    [什么时候 表达 然后 表达]...
    [else 表达]
end

参数

案件

begins the 案件 表达.

什么时候

先于搜索条件或要比较的表达式。

search_condition

is used to set conditions for the results that are selected. Search conditions for 案件 表达s are similar to the search conditions in a where clause. Search conditions are detailed in the Transact-SQL User?s Guide.

然后

precedes the 表达 that specifies a result value of 案件.

表达

is a column name, a constant, a function, a subquery, or any combination of column names, constants, and functions connected by arithmetic or bitwise operators. For more information about 表达s, see ?表达s? in.

select disaster, 
       案件
            什么时候 disaster = "earthquake" 
                然后 "stand in doorway"
            什么时候 disaster = "nuclear apocalypse" 
                然后 "hide in basement"
            什么时候 monster = "zombie apocalypse" 
                然后 "hide with Chuck Norris"
            else
                然后 "ask mom"
       end 
  from endoftheworld
0
额外
这对于轻松理解 CASE 语句的语法非常有帮助。谢谢,并+1。
额外 作者 Mike Varosky,