"Condition expected" 尝试嵌套 CASE WHEN THEN 语句时出错
"Condition expected" error when trying to nest CASE WHEN THEN statements
我修改前的代码(运行良好):
CASE WHEN b.EPIdentifier IS NOT NULL THEN b.EndTime
我在尝试实现一些进一步的嵌套 CASE WHEN THEN 逻辑时所做的更改:
CASE WHEN b.EPIdentifier IS NOT NULL THEN
CASE WHEN b.CancelEndTime IS NOT NULL THEN
b.CancelEndTime
ELSE CASE WHEN b.ExtendedEndDate IS NOT NULL THEN
b.ExtendedEndDate
ELSE CASE WHEN b.EndTime IS NOT NULL THEN
b.EndTime
ELSE a.EndDate END EndDate
我在最后一个 EndDate
:
上出错
an expression of non-boolean type specified in a context where a condition is expected
我不明白为什么会收到错误消息。我试着用 AS EndDate
代替,但我仍然遇到同样的错误。我把嵌套搞砸了吗?我什么也看不到。
您必须在每个 CASE WHEN
上放置一个 END
。你的表情应该是这样的:
CASE WHEN b.EPIdentifier IS NOT NULL THEN
CASE WHEN b.CancelEndTime IS NOT NULL THEN b.CancelEndTime
ELSE CASE WHEN b.ExtendedEndDate IS NOT NULL THEN b.ExtendedEndDate
ELSE CASE WHEN b.EndTime IS NOT NULL THEN b.EndTime END
END
END
ELSE a.EndDate END EndDate
无论如何,您可以使用 COALESCE:
来简化表达式
CASE WHEN b.EPIdentifier IS NOT NULL THEN COALESCE(b.CancelEndTime, b.ExtendedEndDate, b.EndTime)
ELSE a.EndDate END EndDate
来自 MSDN:
The COALESCE expression is a syntactic shortcut for the CASE expression. That is, the code COALESCE(expression1,...n) is rewritten by the query optimizer as the following CASE expression:
CASE
WHEN (expression1 IS NOT NULL) THEN expression1
WHEN (expression2 IS NOT NULL) THEN expression2
...
ELSE expressionN
END
END is the Part of CASE Statement, so you have to lookup for END.
CASE 语句的语法:
SQL CASE
Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
我修改前的代码(运行良好):
CASE WHEN b.EPIdentifier IS NOT NULL THEN b.EndTime
我在尝试实现一些进一步的嵌套 CASE WHEN THEN 逻辑时所做的更改:
CASE WHEN b.EPIdentifier IS NOT NULL THEN
CASE WHEN b.CancelEndTime IS NOT NULL THEN
b.CancelEndTime
ELSE CASE WHEN b.ExtendedEndDate IS NOT NULL THEN
b.ExtendedEndDate
ELSE CASE WHEN b.EndTime IS NOT NULL THEN
b.EndTime
ELSE a.EndDate END EndDate
我在最后一个 EndDate
:
an expression of non-boolean type specified in a context where a condition is expected
我不明白为什么会收到错误消息。我试着用 AS EndDate
代替,但我仍然遇到同样的错误。我把嵌套搞砸了吗?我什么也看不到。
您必须在每个 CASE WHEN
上放置一个 END
。你的表情应该是这样的:
CASE WHEN b.EPIdentifier IS NOT NULL THEN
CASE WHEN b.CancelEndTime IS NOT NULL THEN b.CancelEndTime
ELSE CASE WHEN b.ExtendedEndDate IS NOT NULL THEN b.ExtendedEndDate
ELSE CASE WHEN b.EndTime IS NOT NULL THEN b.EndTime END
END
END
ELSE a.EndDate END EndDate
无论如何,您可以使用 COALESCE:
来简化表达式CASE WHEN b.EPIdentifier IS NOT NULL THEN COALESCE(b.CancelEndTime, b.ExtendedEndDate, b.EndTime)
ELSE a.EndDate END EndDate
来自 MSDN:
The COALESCE expression is a syntactic shortcut for the CASE expression. That is, the code COALESCE(expression1,...n) is rewritten by the query optimizer as the following CASE expression:
CASE
WHEN (expression1 IS NOT NULL) THEN expression1
WHEN (expression2 IS NOT NULL) THEN expression2
...
ELSE expressionN
END
END is the Part of CASE Statement, so you have to lookup for END.
CASE 语句的语法: SQL CASE
Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END