简体   繁体   English

Oracle Apex 5.1动态SQL

[英]Oracle Apex 5.1 Dynamic SQL

how to Dynamic SQL before report to users in Apex 5.1? 在Apex 5.1中向用户报告之前如何动态SQL? 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. :Param1对于用户是可选的。

I want if :Param1 is null then my query execute without any where clause. 我想如果:Param1为null,那么我的查询将执行而没有任何where子句。 otherwise set where clause in my query. 否则在查询中设置where子句。

the point is my Apex is Version 5.1 关键是我的Apex是5.1版

Thanks 谢谢

If that query is used as aa source for a report region, then you don't have to use dynamic SQL, but a simple OR condition: 如果该查询用作报告区域的源,那么您不必使用动态SQL,只需使用简单的OR条件:

select * 
from tb1 t
where (t.name = :param1 or :param1 is null)

The same would work elsewhere too. 在其他地方也一样。

you can use such a way below by using rowtype and type keywords for whole row and column values respectively : 您可以在下面分别通过对whole rowcolumn值使用rowtypetype关键字来使用这种方式:

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 . 因为只有一个参数,所以:Param1i_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. 如果我们有多个参数,则所有参数都应按照sql字符串中绑定参数的出现顺序与using列表中的逗号分隔变量(例如i_name, ....进行i_name, ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM