Reputation: 41
how to Dynamic SQL before report to users in Apex 5.1? my query is:
declare
q varchar2(4000);
begin
q := 'select * from tb1 t';
if :Param1 is not null then
q := q || ' where t.name = :Param1';
end if;
return q;
end;
:Param1 is optional for users.
I want if :Param1 is null then my query execute without any where clause. otherwise set where clause in my query.
the point is my Apex is Version 5.1
Thanks
Upvotes: 0
Views: 801
Reputation: 65228
you can use such a way below by using rowtype
and type
keywords for whole row
and column
values respectively :
SQL> set serveroutput on
SQL> declare
q varchar2(4000);
rt tb1%rowtype;
i_name tb1.name%type := 'Mohammad';
o_surname tb1.surname%type;
begin
q := 'select * from tb1 t';
if i_name is not null then
q := q || ' where ( t.name = :Param1 or :Param1 is null )';
end if;
execute immediate q into rt using i_name, i_name;
-- "i_name" appears twice because of ":Param1" appears twice in the string "q"
o_surname := rt.surname;
dbms_output.put_line(rt.surname);
end;
Taleshi
since there's only one parameter, then :Param1
is matched with i_name
. If we had more than one parameter, then all should be matched in the appearance order of the bind parameters in the sql string with comma seperated variables such as i_name, ....
in the using
list.
Upvotes: 0
Reputation: 142713
If that query is used as a a source for a report region, then you don't have to use dynamic SQL, but a simple OR
condition:
select *
from tb1 t
where (t.name = :param1 or :param1 is null)
The same would work elsewhere too.
Upvotes: 1