简体   繁体   English

在具有参数的 where 子句中使用 CASE 语句

[英]Using CASE statement in where clause having parameters

I am creating oracle report where if the user inputs true or false, the respective values should go to parameter.我正在创建 oracle 报告,如果用户输入 true 或 false,则相应的值应该 go 到参数。 if user inputs All, then both true and false should go to parameters.如果用户输入 All,那么 true 和 false 都应该 go 到参数。

I used the following case statements in 3 ways.我以 3 种方式使用了以下 case 语句。 all of them show missing keyword error.他们都显示缺少关键字错误。 could you please help me resolve this.你能帮我解决这个问题吗?

WHERE "EngExec Status" = ((:eng_exec_status = 'True' and "EngExec Status" = 'True') 
                           OR (:eng_exec_status = 'False' AND "EngExec Status" = 'False') 
                           OR (:eng_exec_status = 'All' AND  ("EngExec Status" = 'True' OR "EngExec Status"= 'False')))
                         
WHERE "EngExec Status" IN (CASE 
                               WHEN (:eng_exec_status) = 'True' THEN "EngExec Status"= 'True'
                               WHEN (:eng_exec_status) = 'False' THEN "EngExec Status"= 'False'
                               ELSE "EngExec Status" IN ('True', 'False')  
                           END) 

WHERE "EngExec Status" IN (CASE 
                               WHEN (:eng_exec_status) = 'All' THEN "EngExec Status" IN ('True','False')
                               ELSE (:eng_exec_status)   
                           END)         

My guess is that you want something simple like我的猜测是你想要一些简单的东西

WHERE ( :eng_exec_status = 'All' )
   OR ( :eng_exec_status = "EngExec Status" )

Your first snippet:你的第一个片段:

WHERE "EngExec Status" = ((:eng_exec_status = 'True' and "EngExec Status" = 'True') 
                           OR (:eng_exec_status = 'False' AND "EngExec Status" = 'False') 
                           OR (:eng_exec_status = 'All' AND  ("EngExec Status" = 'True' OR "EngExec Status"= 'False')))

Fails as you have WHERE column_value = (<boolean_expression>) and that is not valid syntax.失败,因为你有WHERE column_value = (<boolean_expression>)这不是有效的语法。 You can just remove the "EngExec Status" = at the start:您可以在开始时删除"EngExec Status" =

WHERE (:eng_exec_status = 'True'  AND "EngExec Status" = 'True') 
OR    (:eng_exec_status = 'False' AND "EngExec Status" = 'False') 
OR    (:eng_exec_status = 'All'   AND ("EngExec Status" = 'True' OR "EngExec Status"= 'False'))

The second snippet has the CASE expression:第二个片段具有CASE表达式:

CASE 
WHEN (:eng_exec_status) = 'True' THEN "EngExec Status"= 'True'
WHEN (:eng_exec_status) = 'False' THEN "EngExec Status"= 'False'
ELSE "EngExec Status" IN ('True', 'False')  
END

It should have the syntax:它应该具有以下语法:

CASE WHEN comparison_expr THEN return_expr [...] ELSE expr END

And from the documentation : 从文档中

For both simple and searched CASE expressions, all of the return_exprs must either have the same data type ( CHAR , VARCHAR2 , NCHAR , or NVARCHAR2 , NUMBER , BINARY_FLOAT , or BINARY_DOUBLE ) or must all have a numeric data type.对于简单和搜索的CASE表达式,所有return_exprs必须具有相同的数据类型( CHARVARCHAR2NCHARNVARCHAR2NUMBERBINARY_FLOATBINARY_DOUBLE )或都必须具有数字数据类型。

Your return values do not have any of those data types and you have put a comparison expression in instead.您的返回值没有任何这些数据类型,而是您已将比较表达式放入其中。

Oracle complains about a " missing keyword error" as it expects either the WHEN , ELSE or END keywords after the "EngExec Status" in the THEN and ELSE clauses (since it is not valid to put a comparison expression in that location so the = and IN operators and the expression following them are invalid syntax). Oracle 抱怨“缺少关键字错误”,因为它在THENELSE子句中的"EngExec Status"之后需要WHENELSEEND关键字(因为在该位置放置比较表达式是无效的,因此=IN运算符及其后面的表达式是无效的语法)。


Your third snippet fails for a similar reason to the second.您的第三个片段因与第二个类似的原因而失败。


If you have a CHECK constraint on the "EngExec Status" column to restrict the value to either 'True' or 'False' then you can simplify the WHERE filter to:如果您对"EngExec Status"列有CHECK约束以将值限制为'True''False'那么您可以将WHERE过滤器简化为:

WHERE :eng_exec_status = "EngExec Status" OR :eng_exec_status = 'All'

If you do not have a CHECK constraint to restrict the values then you can use:如果您没有CHECK约束来限制值,那么您可以使用:

WHERE "EngExec Status" IN ('True', 'False')
AND   (:eng_exec_status = "EngExec Status" OR :eng_exec_status = 'All')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM