2016-03-03 49 views
1

请问有人可以帮助解决SQL问题吗? 我有一个查询目前接受一个外部参数,可以是空值或单个值,如'STR1'。如何在正则表达式产品上使用nvl

这通过查询在格式下面处理:

select a.refe_id from a, b, c 
where a.owln_code = c.code 
and c.con_id = b.pers_id 
and b.con_abbrv = nvl(:PARAM, b.con_abbrv) 
and a.status = '01' 

在将来的外部参数可以具有一个单一的值,null或多个值由管e.g STR1分离| STR2 | STR3 |

我现在处于下面的阶段,以适应这一点,但已经失去了允许通过表b中的所有行进行匹配(如果参数为null)的nvl测试。 请有人可以帮助如何做到这一点?我已经搜索了网络,并试图自己做。首先十分感谢。

select a.refe_id from a, b, c 
where a.owln_code = c.code 
and c.con_id = b.pers_id 
and regexp_like(:PARAM, '(^|)' || b.con_abbrv|| '(|$)') 
and a.status = '01' 

回答

0

我建议是这样的:

select a.refe_id from a, b, c 
where a.owln_code = c.code 
    and c.con_id = b.pers_id 
    and (:PARAM IS NULL OR regexp_like(:PARAM, '(^|)' || b.con_abbrv|| '(|$)')) 
    and a.status = '01' 

没有关系,但我也建议使用显式联接语法:我想你会发现它使查询更容易,因为阅读它将JOIN逻辑从WHERE逻辑中分离出来:

select a.refe_id from a 
join c on a.owln_code = c.code 
join b on c.con_id = b.pers_id 
where (:PARAM IS NULL OR regexp_like(:PARAM, '(^|)' || b.con_abbrv|| '(|$)')) 
    and a.status = '01' 
+0

啊,是的,在所有情况下都能很好地工作。埃德做得非常好,非常感谢。我会标记你的答案是正确的。保重,戴夫 – MrTea