我有一个标准搜索子句,我在某些过滤器上选择记录,例如描述和状态,状态值为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表达式?
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)
答案 0 :(得分:3)
我更像是一个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)