简体   繁体   English

Oracle where子句中的案例表达

[英]Case expression in Oracle where clause

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. 我有一个标准的搜索子句,据此我可以在某些过滤器(例如描述和状态)上选择记录,状态值为101到110。状态可以为null,如果这样,我将返回任何状态的记录。 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子句是:

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? 在where子句中使用case表达式可以做到吗?


@Damien provided the answer so thanks to him for that. @Damien提供了答案,因此感谢他。 There is another scenario I need to cater for - the same proc returns individual as well as multiple records. 我还需要解决另一种情况-同一proc返回单个以及多个记录。 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: 如果有人搜索状态为“忽略”的单个记录(p_cae_sec_id_n不为null),则该记录被排除在上面,因此我将其添加到下面:

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'm more of a SQL Server guy, but the following should do the trick (assuming Oracle's not equal to is <> , not != ): 我更像是一个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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM