【问题标题】:Case expression in where clause PL/SQLwhere 子句 PL/SQL 中的 case 表达式
【发布时间】:2014-06-24 08:57:31
【问题描述】:

如何在 PL/SQL 中查询类似的内容?

declare
  l_batch_type number := 1;
select
  *
from table t
where 
  case 
    when l_batch_type = 1 then ('A', 'B')
    when l_batch_type = 2 then ('C', 'D')
    when l_batch_type = 3 then ('E', 'F')
    when l_batch_type is null then null end in t.batch_type;

我收到 SQL 错误:ORA-00907:缺少右括号 ('X','Y') 值和运算符显然有问题, 但我不知道如何匹配正确的语法。

谢谢!

【问题讨论】:

  • 您实际上想用('A', 'B') 实现什么?检查 batch_type 是否是其中之一?

标签: oracle plsql case where


【解决方案1】:

您通常希望使用简单的布尔逻辑,而不是将 case 语句放在 where 子句中

where (l_batch_type = 1 and t.batch_type in ('A', 'B'))
   or (l_batch_type = 2 and t.batch_type in ('C', 'D'))
   or (l_batch_type = 3 and t.batch_type in ('E', 'F'))

由于null 永远不会等于任何东西(也永远不会不等于任何东西),我不确定您的l_batch_type is null 条件是否旨在向逻辑添加任何东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多