【问题标题】: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