我有一个标准的搜索子句,在某些过滤器上选择记录,比如描述和状态,状态值为101到110。状态可能为null,如果为null,则返回任何状态的记录。但是,我现在有一个新的状态,在没有特定状态时,必须从返回的记录中排除,只有在特定选择时才返回。因此,基于特定状态的搜索只返回该状态,没有特定状态的搜索返回除新状态之外的所有状态。原有的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给出了答案,感谢他的回答。还有另一种情况,我需要迎合-相同的proc返回个人以及多个记录。如果有人搜索具有忽略状态的单个记录(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)发布于 2011-03-01 09:07:16
我更喜欢Server,但下面的内容应该可以做到(假设Oracle不等于is <>,而不是!=):
(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)https://stackoverflow.com/questions/5152294
复制相似问题