CASEcase_value
WHENwhen_value
THENstatement_list
[WHENwhen_value
THENstatement_list
] ... [ELSEstatement_list
] END CASE
Or:或:
CASE WHENsearch_condition
THENstatement_list
[WHENsearch_condition
THENstatement_list
] ... [ELSEstatement_list
] END CASE
The 存储程序的CASE
statement for stored programs implements a complex conditional construct.CASE
语句实现了一个复杂的条件构造。
There is also a 还有一个CASE
operator, which differs from the CASE
statement described here. CASE
运算符,它与这里描述的CASE
语句不同。See Section 12.5, “Flow Control Functions”. 请参阅第12.5节,“流程控制函数”。The CASE
statement cannot have an ELSE NULL
clause, and it is terminated with END CASE
instead of END
.CASE
语句不能有ELSE NULL
子句,并且它以END CASE
而不是END
终止。
For the first syntax, 对于第一种语法,case_value
is an expression. case_value
是一个表达式。This value is compared to the 将此值与每个when_value
expression in each WHEN
clause until one of them is equal. WHEN
子句中的when_value
表达式进行比较,直到其中一个相等。When an equal 当找到等于when_value
is found, the corresponding THEN
clause statement_list
executes. when_value
时,将执行相应的THEN
子句statement_list
。If no 如果没有when_value
is equal, the ELSE
clause statement_list
executes, if there is one.when_value
相等,则执行ELSE
子句statement_list
(如果有)。
This syntax cannot be used to test for equality with 此语法不能用于测试是否与NULL
because NULL = NULL
is false. NULL
相等,因为NULL
=NULL
为false
。See Section 3.3.4.6, “Working with NULL Values”.请参阅第3.3.4.6节,“使用NULL值”。
For the second syntax, each 对于第二种语法,每个WHEN
clause search_condition
expression is evaluated until one is true, at which point its corresponding THEN
clause statement_list
executes. WHEN
子句search_condition
表达式都被求值,直到有一个表达式为true
,此时执行相应的THEN
子句
。statement_list
If no 如果没有search_condition
is equal, the ELSE
clause statement_list
executes, if there is one.search_condition
相等,则执行ELSE
子句statement_list
(如果有)。
If no 如果没有when_value
or search_condition
matches the value tested and the CASE
statement contains no ELSE
clause, a Case not found for CASE statement error results.when_value
或search_condition
与测试的值匹配,并且CASE
语句不包含ELSE
子句,则产生错误结果Case not found for CASE statement。
Each 每个statement_list
consists of one or more SQL statements; an empty statement_list
is not permitted.statement_list
由一个或多个SQL语句组成;不允许使用空statement_list
。
To handle situations where no value is matched by any 若要处理没有值与任何WHEN
clause, use an ELSE
containing an empty BEGIN ... END
block, as shown in this example. WHEN
子句匹配的情况,请使用包含空BEGIN ... END
块的ELSE
,如本例所示。(The indentation used here in the (此处在ELSE
clause is for purposes of clarity only, and is not otherwise significant.)ELSE
子句中使用的缩进仅为清楚起见,在其他方面并不重要。)
DELIMITER | CREATE PROCEDURE p() BEGIN DECLARE v INT DEFAULT 1; CASE v WHEN 2 THEN SELECT v; WHEN 3 THEN SELECT 0; ELSE BEGIN END; END CASE; END; |