Oracle APEX • PL/SQL • Dynamic Reporting
Have you ever needed to build a dashboard where users select a report from a dropdown list, and the page dynamically renders the query results? A common design pattern is to centralize your report queries in a database table:
However, if you've tried implementing this with an Interactive Report or Interactive Grid, you likely ran into database metadata conflicts or compilation errors when switching between reports.
In this post, we will explore why standard reports struggle with dynamic structures and provide a step-by-step guide to building a fully dynamic report viewer using Classic Reports and Generic Columns in Oracle APEX.
The Challenge: Fixed Metadata vs. Dynamic Columns
Oracle APEX Interactive Reports (IR) and Interactive Grids (IG) are highly interactive tools that allow end-users to sort, filter, and group data. To provide this functionality, APEX wraps your query in outer SQL statements. This requires APEX to know the exact column names, data types, and column counts at page design time.
If your stored queries return different columns (e.g., Query A returns 3 columns, Query B returns 8 columns), IR and IG will fail with ORA-00904: invalid identifier errors.
COL01, COL02 ... COL60), allowing the page to render any valid SQL query on the fly.
Step-by-Step Implementation Guide
Step 1: Create the Select List Item
First, create a Select List item on your page (we'll name it P1_REPORT_SELECT) to let users choose which report to display.
- Type: Select List
- List of Values > Type: SQL Query
- SQL Query:
Step 2: Create the Classic Report Region
Create a new region on your page where the dynamic data will be displayed.
- Title: Dynamic Report Viewer
- Type: Classic Report
- Source > Location: Local Database
- Source > Type: PL/SQL Function Body returning SQL Query
- PL/SQL Function Body:
*Note: Remember to add P1_REPORT_SELECT to Page Items to Submit.
Step 3: Configure Generic Columns
- Navigate to the Classic Report's Region tab in the Page Designer.
- Scroll down to the Source section.
- Set Use Generic Column Names to Yes.
- Set Amount of Generic Columns to the maximum number of columns you expect your largest query to return (e.g., 60).
Step 4: Generate Dynamic Column Headers
Since APEX maps the columns to generic names like COL01, your table headers will display as "Col 1", "Col 2". To show the real headers, use `DBMS_SQL` to pull dynamic metadata:
- Go to the Classic Report Attributes tab > Heading section.
- Set Type to PL/SQL Function Body.
- Enter this PL/SQL block:
Step 5: Refresh the Report via AJAX
Make the UI responsive with a Dynamic Action so the report updates instantly when a new selection is made.
- Right-click on
P1_REPORT_SELECTand select Create Dynamic Action. - Name:
Refresh Report - Event: Change
- True Action > Action: Refresh
- True Action > Selection Type: Region
- True Action > Region: [Select your Classic Report Region]
Report Type Comparison Matrix
Here is a comparison of how different report types in Oracle APEX handle dynamic SQL structures:
| Feature / Capability | Classic Report (Generic) | Interactive Report (IR) | Interactive Grid (IG) |
|---|---|---|---|
| Supports Dynamic Columns? | Yes | No | No |
| Dynamic Column Headers? | Yes (via PL/SQL) | No | No |
| End-User Filters & Sorting? | Manual Coding Only | Yes | Yes |
| Data Editing (DML)? | No | No | Yes |
Security Warnings & Best Practices
To keep your APEX application secure, implement these best practices:
- Authorization Schemes: Restrict the admin screen where queries are edited or created to high-privilege Developer/Admin roles only.
- SQL Parsing Validation: Add a database trigger or validation block to verify that the query text only starts with a
SELECTtoken (and does not contain DDL operations likeDROPorALTER). - Read-only DB Schema: Where possible, have the APEX parsing schema execute queries in a read-only context.
Conclusion
By using Classic Reports and the Generic Columns feature, APEX developers can bypass database compile-time design constraints and build highly flexible, dynamic dashboard tools. This minimizes page count, keeping your workspace clean and centralized.
Comments
Post a Comment