Sql 游标where子句中的可选参数
我有下面的示例查询,它从过程参数中获取值。参数可以传递,也可以默认为nullSql 游标where子句中的可选参数,sql,oracle,plsql,plsqldeveloper,plsql-package,Sql,Oracle,Plsql,Plsqldeveloper,Plsql Package,我有下面的示例查询,它从过程参数中获取值。参数可以传递,也可以默认为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
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用户可以输入术语代码而不输入任何其他参数,也可以输入术语代码和表格详细信息而不输入任何其他输入参数
其他2个或多个条件也是如此
如果传递了一个术语代码,而table_DETL_代码为null,则查询应返回该术语代码的所有值,而此查询返回null
在PL/SQL中,有没有一种不使用case或if条件的方法来实现这一点 如果我理解正确,这可能就是你想要的:
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)
如果我理解正确,这可能就是你想要的:
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)
说明似乎要求用户输入表\u term\u代码,然后输入其他3项中的一项或全部。如果是,那么也许:
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)
);
说明似乎要求用户输入表\u term\u代码,然后输入其他3项中的一项或全部。如果是,那么也许:
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)
);
Oracle具有内置的空处理函数。请参阅A Abra-我确实尝试了NVL,但我的问题是,即使表_DETL_代码不为null,那么我的查询也应该过滤第二个条件,而不是where子句在术语_CODE中为我提供所有记录。Oracle具有内置的null处理函数。请参阅A Abra-我确实尝试了NVL,但我的问题是,即使表_DETL_代码不为空,那么我的查询也应该过滤第二个条件,而不是where子句以术语_代码提供所有记录。谢谢。在上面的例子中,如果我为term_代码传入值,而不为其余的传递值。当和运算符查看空值时,它不会返回记录。如果用户只传递第一个字段的记录,我的查询应该返回记录。OR运算符将返回,但我的第二个场景需要过滤记录中的term_代码和NOT null detl_代码将失败。谢谢。在上面的例子中,如果我为term_代码传入值,而不为其余的传递值。当和运算符查看空值时,它不会返回记录。如果用户只传递第一个字段的记录,我的查询应该返回记录。OR运算符将返回,但我的第二个场景(需要过滤记录中的term_代码和NOT null detl_代码)将失败。