oracle中的条件表达式 case 表达式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/uotail/article/details/83313196


https://docs.oracle.com/cd/B19306_01/server.102/b14200/expressions004.htm
CASE expressions let you use IF ... THEN ... ELSE logic in SQL statements without having to invoke procedures. The syntax is:
CASE表达式可以让你在SQL语句中使用IF... THEN... ELSE的逻辑,而不必调用过程。它的语法是如下

CASE 
     {
simple_case_expression|searched_case_expression}
     [
else_clause ]
END 

其中simple_case_expression :: =     是指

 expr WHEN comparison_expr THEN return_expr
     [ WHEN comparison_expr THEN return_expr ]...

searching_case_expression :: = 

WHEN condition THEN return_expr
[ WHEN condition THEN return_expr ]...

else_clause :: =

else_clause.gif的描述如下

 ELSE else_expr

In a simple CASE expression, Oracle Database searches for the first WHEN ... THEN pair for which expr is equal to comparison_expr and returns return_expr. If none of the WHEN ... THEN pairs meet this condition, and an ELSE clause exists, then Oracle returns else_expr. Otherwise, Oracle returns null. You cannot specify the literal NULL for every return_expr and the else_expr.

在一个简单的CASE表达式中,oracle数据库首先搜索第一组 WHEN ... THEN ,如果expr 等于comparison_expr 则返回return_expr,

如果没有匹配到并且存在ELSE 表达式,则返回else_expr,否则返回null。

因此你不能为每个return_expr和else_expr定义null的返回值。这样会产生歧义。

In a searched CASE expression, Oracle searches from left to right until it finds an occurrence of condition that is true, and then returns return_expr. If no condition is found to be true, and an ELSE clause exists, Oracle returns else_expr. Otherwise, Oracle returns null.

Oracle Database uses short-circuit evaluation. That is, for a simple CASE expression, the database evaluates each comparison_expr value only before comparing it to expr, rather than evaluating all comparison_expr values before comparing any of them with expr. Consequently, Oracle never evaluates a comparison_expr if a previous comparison_expr is equal to expr. For a searched CASE expression, the database evaluates each condition to determine whether it is true, and never evaluates a condition if the previous condition was true.

For a simple CASE expression, the expr and all comparison_expr values must either have the same datatype (CHARVARCHAR2NCHAR, or NVARCHAR2NUMBERBINARY_FLOAT, or BINARY_DOUBLE) or must all have a numeric datatype. If all expressions have a numeric datatype, then Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that datatype, and returns that datatype.

For both simple and searched CASE expressions, all of the return_exprs must either have the same datatype (CHARVARCHAR2NCHAR, or NVARCHAR2NUMBERBINARY_FLOAT, or BINARY_DOUBLE) or must all have a numeric datatype. If all return expressions have a numeric datatype, then Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that datatype, and returns that datatype.

The maximum number of arguments in a CASE expression is 255. All expressions count toward this limit, including the initial expression of a simple CASE expression and the optional ELSE expression. Each WHEN ... THEN pair counts as two arguments. To avoid exceeding this limit, you can nest CASE expressions so that the return_expr itself is a CASE expression.

猜你喜欢

转载自blog.csdn.net/uotail/article/details/83313196
今日推荐