Oracle APEX使用PL/SQL function返回select语句进行动态查询

  • How to create a report that's based on a dynamic query

   Table of Content

IntroductionIn HTML DB a report is simply the formatted result of a SQL query. You can generate reports by:

  • Selecting and running a built-in wizard
  • Defining a report region based on a SQL query
  • Defining a report region based on a PL/SQL function returning a SQL query

Certain applications require that report results are based on user input (see Figure 1). Typically, this requires a dynamic query which incorprates session state values. In this How To, we will create a report based on a PL/SQL function which generates a SQL Query at runtime.

Figure 1 - Ordered Products

Ordered Products

Software Requirements

  • Oracle HTML DB
  • Web browser such as Netscape 7+ or Internet Explorer 5.5+

Create a Dynamic Query Report

We will extend Sample Application that is included when a new workspace is provisioned. To create a dynamic query report, follow these steps:

  • In the 'Sample Application', create a Page as follows:
    • navigate to the Application Builder
    • click on Extend this Application link in the Tasks region

      Figure 2 - Tasks

      Tasks

    • select Extend this Application with a blank page
    • Enter 600 as page number
    • Enter 'Ordered Products' as the page name
    • Select 'Do not use tabs' for the new page
    • Click Finish to create the page
    • Click Edit Page once you get to the success page
  • Create a dynamic query report using the following steps:
    • Navigate to the page definition for page 600
    • Under Regions, click Create
    • Choose Report as region type
    • Choose PL/SQL Function Returning SQL Query as the report implementation type
    • enter 'Ordered Products' as the region title and accept the default values for all other prompts
    • Enter following PL/SQL function body:
    • declare 
        q varchar2(4000);
      begin 
        q:='   select p.category, ';
        q:=q||'       p.product_name, ';
        q:=q||'       i.quantity, ';
        q:=q||'       i.unit_price ';  
        q:=q||'  from demo_product_info p, ';
        q:=q||'       demo_order_items i ';
        q:=q||' where p.product_id = i.product_id ';
       
        if :p600_show != 'ALL' then
          q:=q||'and p.category = :p600_show';
        end if;
      
        return q;
      end;
    • click the Create Region button to create the report

Add Item and Button

The PL/SQL function returning a SQL Query we are using above uses an page item. Follow these steps to create the page item:

  • Under Items, click Create
  • Enter P600_SHOW as item name, choose 'Select List' for Display As
  • Enter '- All Categories-' as NULL text
  • Enter 'ALL' as NULL value
  • Enter the following List of Values Query:
  • select distinct category a, category b 
    from demo_product_info
    order by 1
  • Enter 'Show' as label
  • Select 'Not Required Label' label template
  • Click Create Item button to complete the creation of the item

For the report to be driven by the product category select list, we need to submit the page. To make this happen, we will add a button as follows:

  • Under Buttons, click Create
  • select Create a button displayed among this region's items
  • enter P600_GO as button name
  • enter 'themes/opal/go.gif' as the button image name
  • click Create Button to complete the creation of this button

Now you can run the page, choosing different product categories to control the output of ths report. If you wish to enhance the look of the report, read the Customizing HTML DB Reports How To.

转载自:

http://www.oracle.com/technetwork/developer-tools/apex/dynamic-report-092026.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值