Oracle:Where子句中的案例

时间:2014-01-03 02:29:08

标签: sql oracle oracle-sqldeveloper plsqldeveloper

如何在Oracle查询中实现此目的。我在IN子句中错过了表达式错误

  WHERE TBL_DTL_HOST.HOST_METHOD = 'CCtxnPostRq'
AND TRUNC(TBL_DTL_FEATURE.START_DATETIME)  BETWEEN TO_DATE (i_startdate, 'DD/MM/YYYY') AND     TO_DATE (i_enddate,'DD/MM/YYYY')
AND (SELECTED_DNIS IS NULL OR TBL_DTL_FEATURE.DNIS = SELECTED_DNIS)
AND (TEMP_CUSTOMER_ID IS NULL OR TBL_DTL_FEATURE.CUSTOMER_ID = TEMP_CUSTOMER_ID)
AND     
(CASE WHEN i_Feature='All' 
THEN (TBL_DTL_HOST.FEATURE_ID IN ('F020','F021'))
ELSE (TBL_DTL_HOST.FEATURE_ID IN ('F020'));
END)
我错过了什么..?任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:2)

Oracle不像其他表达式那样处理布尔表达式 - 它将它们作为语法而不是类型处理 - 因此CASE表达式无法评估为布尔值。

在你的情况下,我认为最清晰的代码是你只需重写一下:

AND (    TBL_DTL_HOST.FEATURE_ID = 'F020'
      OR (i_Feature = 'All' AND TBL_DTL_HOST.FEATURE_ID = 'F021')
    )

答案 1 :(得分:0)

该条款的最后一部分应该像这样正确评估

(CASE WHEN i_Feature='All' AND (TBL_DTL_HOST.FEATURE_ID IN ('F020','F021')) THEN 1
WHEN i_Feature<>'All' AND (TBL_DTL_HOST.FEATURE_ID IN ('F020')) THEN 1
ELSE 0
END) = 1