ORA-00905: where case 语句中缺少关键字

ORA-00905: missing keyword in where case statement

select m_seqe
      ,m_emai
      ,m_phon
      ,e_seqe
      ,m_id
      ,
      ( case 
        when (:M_STAT = 0 )  and (m_id is not null ) then 'a'
        when (:M_STAT = 0 )  and (m_id is null  ) then 'b'
        when (:M_STAT = 200 ) then 'c'
        Else ' - ' end ) as Stat
from m_users 
where m_stat = :M_STAT   
and  m_id =
case when (:M_ID = 0 ) then m_id is null
     when (:M_ID ='a') then m_id is not null
else 'do nothing' end 

按照你的说法,我明白你在说什么,那将是几个 OR 条件的组合:

 WHERE   m_stat = :M_STAT
   AND (   ( m_id IS NULL     AND :m_id = 0)
        OR ( m_id IS NOT NULL AND :m_id = 'a')
        OR ( m_id = 'do nothing'
       );

您不能使用 case 表达式来确定过滤器中的逻辑;所以这没有意义,这就是解析器所抱怨的:

and  m_id =
case when (:M_ID = 0 ) then m_id is null
     when (:M_ID ='a') then m_id is not null
else 'do nothing' end 

您可能有一个 case 表达式,它的计算结果是一个值,然后您可以将该值与您的列值进行比较,但它在这里似乎也没有意义,尤其是因为 null 处理有点笨拙。

您似乎想用简单的布尔逻辑替换它:

where m_stat = :M_STAT   
and ((:M_ID = 0 and m_id is null)
  or (:M_ID ='a' and m_id is not null))

或者在将绑定变量视为字符串时保持一致:

where m_stat = :M_STAT   
and ((:M_ID = '0' and m_id is null)
  or (:M_ID ='a' and m_id is not null))

没有 'else' 担心;您的 'do nothing' 字符串似乎是一个占位符,并不打算实际匹配任何内容,即这不是您要查找的真实 m_id 值)。如果给定行的两个组合条件都不为真,则总体条件为假,并且该行不会匹配,它将被过滤掉。

你的问题比较模糊,但可以按如下方式实现

select m_seqe
      ,m_emai
      ,m_phon
      ,e_seqe
      ,m_id
      ,
      ( case 
        when (:M_STAT = 0 )  and (m_id is not null ) then 'a'
        when (:M_STAT = 0 )  and (m_id is null  ) then 'b'
        when (:M_STAT = 200 ) then 'c'
        Else ' - ' end ) as Stat
from m_users 
where m_stat = :M_STAT and 
        (
            case when when (m_id IS NOT NULL AND :m_id = 'a') then 'a'
            when (m_id IS NULL AND :M_ID = 0 ) then 'b'
            when (m_id = 'do nothing') then 'c'
            when 'd' end 
        ) in ('a','b','c')