Oracle where 子句中的 case 表达式
我有一个标准搜索子句,通过该子句,我可以选择某些过滤器上的记录,例如描述和状态,状态值为 101 到 110。状态可能为空,如果是这样,我将返回任何状态的记录。但是,我现在有一个新状态,当没有特定状态时,必须将其从返回的记录中排除,并且仅在专门选择时才返回。因此,基于特定状态的搜索仅返回该状态,没有特定状态的搜索将返回除新状态之外的所有状态。原来的where子句是:
where Upper(cfs.CAE_SEC_ID) = Upper(NVL(p_cae_sec_id_n,cfs.CAE_SEC_ID))
and Upper(SEC_CODE) like '%' || Upper(NVL(p_fm_sec_code_c,SEC_CODE)) || '%'
and APPR_STATUS = NVL(p_appr_status, APPR_STATUS)
order by appr_status DESC, cae_sec_id
我现在想做的是这样的:
where Upper(cfs.CAE_SEC_ID) = Upper(NVL(p_cae_sec_id_n,cfs.CAE_SEC_ID))
and Upper(SEC_CODE) like '%' || Upper(NVL(p_fm_sec_code_c,SEC_CODE)) || '%'
and APPR_STATUS =
(CASE WHEN p_appr_status is null THEN --return all statuses except 110
WHEN p_appr_status is not null THEN (p_appr_status)
END)
order by appr_status DESC, cae_sec_id
在where子句中使用case表达式可以吗?
@Damien 提供了答案,非常感谢他。我需要满足另一种情况 - 同一过程返回单个记录以及多个记录。如果有人搜索状态为忽略的单个记录(p_cae_sec_id_n 不为空),则该记录已从上面排除,因此我将其添加到下面:
and APPR_STATUS =
(CASE WHEN p_appr_status is null and APPR_STATUS != 110 THEN APPR_STATUS
WHEN (p_appr_status is null and p_cae_sec_id_n is not null) THEN APPR_STATUS
WHEN p_appr_status is not null THEN (p_appr_status)
END)
I have a standard search clause whereby I'm selecting records on certain filters such as description and status, the status values being 101 to 110. Status may be null, if so I return records of any status. However I now have a new status which has to be excluded from the returned records when there is no specific status, and only returned when specifically selected. So a search based on a specific status returns just that status, a search without a specific status returns all statuses except the new one. The original where clause is:
where Upper(cfs.CAE_SEC_ID) = Upper(NVL(p_cae_sec_id_n,cfs.CAE_SEC_ID))
and Upper(SEC_CODE) like '%' || Upper(NVL(p_fm_sec_code_c,SEC_CODE)) || '%'
and APPR_STATUS = NVL(p_appr_status, APPR_STATUS)
order by appr_status DESC, cae_sec_id
What I now want to do is something like this:
where Upper(cfs.CAE_SEC_ID) = Upper(NVL(p_cae_sec_id_n,cfs.CAE_SEC_ID))
and Upper(SEC_CODE) like '%' || Upper(NVL(p_fm_sec_code_c,SEC_CODE)) || '%'
and APPR_STATUS =
(CASE WHEN p_appr_status is null THEN --return all statuses except 110
WHEN p_appr_status is not null THEN (p_appr_status)
END)
order by appr_status DESC, cae_sec_id
Is this possible with a case expression in the where clause?
@Damien provided the answer so thanks to him for that. There is another scenario I need to cater for - the same proc returns individual as well as multiple records. If someone searches for an individual record (p_cae_sec_id_n is not null) that has a status of ignore then that was being excluded from above, so I've added it in below:
and APPR_STATUS =
(CASE WHEN p_appr_status is null and APPR_STATUS != 110 THEN APPR_STATUS
WHEN (p_appr_status is null and p_cae_sec_id_n is not null) THEN APPR_STATUS
WHEN p_appr_status is not null THEN (p_appr_status)
END)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更喜欢 SQL Server,但以下应该可以解决问题(假设 Oracle 不等于是
<>
,而不是!=
):I'm more of a SQL Server guy, but the following should do the trick (assuming Oracle's not equal to is
<>
, not!=
):