为经典报告添加自定义搜索

时间:2019-04-22 18:33:20

标签: oracle plsql oracle-apex oracle-apex-5.1

我想在经典报表中添加搜索,因此我在报表上方的页面上添加了一个文本框和一个按钮,并在页面上添加了执行PL / SQL代码的流程:

DECLARE
    v_sql varchar2(4000);
BEGIN
IF :P3_SEARCH IS NOT NULL THEN
    v_sql := q'[select field1, field2, field3
           FROM Table1
           WHERE category_id = ]' ||TO_NUMBER(:P3_CATEGORY)||
           q'[ AND (function(id) LIKE '%]'||:P3_SEARCH||| 
           q'[%' OR field2 LIKE '%]' ||:P3_SEARCH|| 
           q'[%' OR field3 LIKE '%]' ||:P3_SEARCH|| q'[%')]';
ELSE
    v_sql := q'[select field1, field2, field3
           FROM Table1
           WHERE category_id = ]' ||TO_NUMBER(:P3_CATEGORY)|| q'[]';
    END IF;

  apex_debug.MESSAGE('my query is: ' || v_sql);      

    --   RETURN v_sql;
END;

我有两个问题-我产生的查询未按预期显示-%的迹象消失了,而不是

select field1, field2, field3
FROM Table1
WHERE category_id = 4
AND (function(id) like '%test%' OR field2 like '%test%' OR field3 like '%test%')

我明白了

select field1, field2, field3
FROM Table1
WHERE category_id = 4
AND (function(id) like 'test' OR field2 like 'test' OR field3 like 'test')

而且,当单击搜索按钮时,我也无法弄清楚如何为报告区域切换源SQL。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

它必须动态吗?因为,该查询中没有 dynamic 。如果是:P3_SEARCH困扰着您,那就有办法了。简化后,它看起来像这样:

select something
from some_table
where some_column = :P3_SEARCH        --> condition A
   or :P3_SEARCH is null;             --> condition B

扩展到您的查询,可以将其重写如下:

select field1, field2, field3
from table1
where category_id = to_number(:P3_CATEGORY)
  and (  
         --> condition A begins here
         -- condition for P3_SEARCH is not null
        (     :P3_SEARCH is not null
          and(   function(id) like '%' || :P3_SEARCH || '%'
              or field2       like '%' || :P3_SEARCH || '%'
              or field3       like '%' || :P3_SEARCH || '%'
             )
        )
        --> condition A ends here  
      or :P3_SEARCH is null        --> condition B
     );

看看是否有帮助。