【问题标题】:Case statement in where clause oraclewhere 子句 oracle 中的 case 语句
【发布时间】:2019-08-19 20:43:28
【问题描述】:

在 where 子句中,我有两个选项 1) status = 'N' and type = '1' 和 2) status = 'Y' and type = '1' 基于参数我需要执行一个选项:

  where 
     case 
        when (carName = :P_PARAMETER) then  status = 'N' and type = '1'
        else status = 'Y' and type = '1'
     end

在执行get error之后,有什么办法可以解决这个问题或其他方法吗?

【问题讨论】:

  • 你得到什么错误?

标签: sql oracle oracle11g oracle10g


【解决方案1】:

你需要重写逻辑:

where type = '1'
  AND ((status = 'N' AND carName = :P_PARAMETER))
        OR status = 'Y')

【讨论】:

    【解决方案2】:

    type = '1' 选项在这两种情况下都很常见,因此您可以将其排除在 CASE 语句之外(也因为尝试从 CASE 语句返回 2 个值在语法上是错误的):

    where 
      type = '1' 
      and
      status = case when carName = :P_PARAMETER then 'N' else 'Y' end
    

    【讨论】:

      【解决方案3】:

      你可以使用decode()函数:

       where type = '1'  -- common for all cases
         and status= decode(carName,:P_PARAMETER,'N','Y')
      

      Demo

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-08
        • 2021-11-12
        • 1970-01-01
        • 2018-11-29
        • 2020-03-26
        • 2017-04-01
        • 1970-01-01
        相关资源
        最近更新 更多