Oracle where 子句中的 case 表达式

发布于 2024-10-20 03:10:42 字数 1259 浏览 3 评论 0原文

我有一个标准搜索子句,通过该子句,我可以选择某些过滤器上的记录,例如描述和状态,状态值为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-27 03:10:42

我更喜欢 SQL Server,但以下应该可以解决问题(假设 Oracle 不等于是 <>,而不是 !=):

 (CASE WHEN p_appr_status is null and APPR_STATUS<>101 THEN APPR_STATUS
  WHEN p_appr_status is not null THEN (p_appr_status)
  END)

I'm more of a SQL Server guy, but the following should do the trick (assuming Oracle's not equal to is <>, not !=):

 (CASE WHEN p_appr_status is null and APPR_STATUS<>101 THEN APPR_STATUS
  WHEN p_appr_status is not null THEN (p_appr_status)
  END)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文