PL/SQL - Optional conditions in where-clause - without dynamic sql?

后端 未结 5 1801
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 13:51

I have a query where not all conditions are necessary. Here\'s an example of what it looks like when all conditions are used:

select num
from (select distinct q.         


        
5条回答
  •  一整个雨季
    2021-02-08 13:59

    I would just do this

    select num
    from (select distinct q.num
           from cqqv q
           where q.bcode = '1234567' --this is variable
                 and q.lb = 'AXCT' --this is variable
                 and q.type = nvl(, q.type)  --this condition ignored because of "type=*" in input
                 and q.edate > sysdate - 30 --this is variable
           order by dbms_random.value()) subq
    where rownum <= 10; --this is variable
    

    One only has to guarantee that the variable-type is null when the q.TYPE filtering is to be ignored.

提交回复
热议问题