sql—游标where子句中的可选参数

1l5u6lss  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(289)

我有下面的示例查询,它从过程参数中获取值。参数可以传递,也可以默认为null。

SELECT * FROM table
              WHERE( table_term_code = '201931'
              OR (table_term_code = '201931' and table_DETL_CODE ='CA02')
              OR ( table_term_code = '201931' and table_ACTIVITY_DATE = sysdate)
              OR ( table_term_code = '201931' and table_SEQNO = NULL));

i、 e用户可以输入术语代码而不输入任何其他参数,也可以输入术语代码和表格详细代码而不输入任何其他参数。
其他两个或多个条件也是如此。
如果传递了一个术语代码,并且表\u detl \u code为null,则查询应返回该术语代码的所有值,而此查询返回null。
在pl/sql中,有没有一种不带case或if条件的方法来实现这一点?

wlp8pajw

wlp8pajw1#

如果我理解正确的话,这可能就是你想要的:

select * 
from your_table
where (table_term_code     = :par_term_code     or :par_term_code     is null)
  and (table_detl_code     = :par_detl_code     or :par_detl_code     is null)
  and (table_activity_date = :par_activity_date or :par_activity_date is null)
  and (table_seqno         = :par_seqno         or :par_seqno         is null)
dxpyg8gm

dxpyg8gm2#

该描述似乎要求用户输入表\术语\代码,然后输入“无”或正好是其他3项中的1项。如果是这样,那么也许:

select * 
  from your_table
 where table_term_code          = :par_term_code 
   and (   (table_detl_code     = :par_detl_code      and :par_activity_date is null and :par_seqno         is null)
        or (table_activity_date = :par_activity_date  and :par_detl_code     is null and :par_seqno         is null)  
        or (table_seqno         = :par_seqno          and :par_detl_code     is null and :par_activity_date is null)
        or (table_seqno         is null               and :par_detl_code     is null and :par_activity_date is null)
       );

相关问题