我有一个采用可选参数的函数/过程。如果提供,我需要使用参数作为游标的条件。如果没有提供,那么我不希望这种情况。可选参数作为游标中的条件(PL/SQL)
下面是一个非常简化的版本是我想出来的:
create or replace procedure check_data
(p_parm1 in varchar2 default null,
p_parm2 in number default null)
is
begin
if (p_parm1 is null && p_parm2 is null) then
for rec in (select col_1, col_2
from table_a)
loop
/*Statements, use rec.col_1 and rec.col_2 */
end loop;
elsif (p_parm1 is null) then
for rec in (select col_1, col_2
from table_a
where /*condition using p_parm2 */)
loop
/*Statements, use rec.col_1 and rec.col_2 */
end loop;
elsif (p_parm2 is null) then
for rec in (select col_1, col_2
from table_a
where /*condition using p_parm1 */)
loop
/*Statements, use rec.col_1 and rec.col_2 */
end loop;
else
for rec in (select col_1, col_2
from table_a
where /*condition using p_parm1 */
and /*condition using p_parm2 */)
loop
/*Statements, use rec.col_1 and rec.col_2 */
end loop;
end if;
end;
有没有办法让光标一次,表示如果不提供参数,忽略哪些条件?
我会试试这个。我建议使用COALESCE函数,并在未提供时将变量设置为通配符,即: v_variable1:= COALESCE(p_parm1,'%'); /*然后在where子句*/ 其中表达式LIKE v_variable1; 但这会导致需要太长时间才能返回的大规模表扫描。我的担心是,采取所有场景的单个查询将具有类似的性能,但我会尝试。 谢谢! – RuinExplorer