📊 From REF CURSOR to Report: Pipelined Functions in Oracle APEX Made Simple
🚩Introduction
Pipelined table functions solve this by converting REF CURSOR output into a format that behaves like a table. This lets you reuse existing procedures while still feeding data into APEX reports using simple SQL, keeping your application clean and easy to maintain.
📑 Why This Approach Is Needed
In Oracle APEX, reports need SQL queries, but many times data may comes from procedures that return REF CURSORS. These cannot be used directly in reports, which makes it harder to show the data without changing existing code.
By using a pipelined table function, you can convert that data into a simple SQL query format. This helps you reuse existing logic, avoid repeating code, and automatically show updated data in reports. It keeps the application simple, flexible, and easier to maintain.
This approach is useful when:
- You need to display data from a REF CURSOR in reports without rewriting existing procedures
You want to reuse PL/SQL logic instead of duplicating queries
Report data should change dynamically based on database values
You want to avoid hardcoding SQL logic in multiple places
The application should remain simple, flexible, and easy to maintain
By using this method, applications become more efficient, easier to manage, and allow seamless integration between PL/SQL logic and SQL-based reports in Oracle APEX with minimal code duplication.
👉 Use Case: Using Pipelined Function for Reports in Oracle APEX
Step 1:
First, we need to create a procedure that returns data using a REF CURSOR in Oracle Database. This cursor will be used later in APEX through a pipelined function.
------ Ref Cursor ------
CREATE OR REPLACE PROCEDURE ref_cursor_proc (p_cursor OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_cursor FOR SELECT deptno, empno, ename, job, sal FROM emp ORDER BY 1, 2;
END;
------ Ref Cursor ------
CREATE OR REPLACE PROCEDURE ref_cursor_proc (p_cursor OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_cursor FOR SELECT deptno, empno, ename, job, sal FROM emp ORDER BY 1, 2;
END;
Step 2:
------ Object Type & Table Type for Pipelined Function ------
CREATE OR REPLACE TYPE emp_obj AS OBJECT (
empno NUMBER,
deptno NUMBER,
ename VARCHAR2(100),
job VARCHAR2(50),
sal NUMBER
);
CREATE OR REPLACE TYPE emp_tab AS TABLE OF emp_obj;
Step 3:
Next, we will create a pipelined table function. This function takes the data from the REF CURSOR, converts each row into an object, and returns it in a table format that can be used directly in Oracle APEX Interactive or Classic Reports.
------ Pipelined Function ------
CREATE OR REPLACE FUNCTION get_emp_pipe
RETURN emp_tab PIPELINED
AS
l_cursor SYS_REFCURSOR;
l_emp_id NUMBER;
deptno NUMBER;
ename VARCHAR2(75);
job VARCHAR2(75);
sal NUMBER;
BEGIN
ref_cursor_proc (l_cursor); -- Calling the Procedure which returns ref cursor
LOOP
FETCH l_cursor INTO l_emp_id, deptno, ename, job, sal;
EXIT WHEN l_cursor%NOTFOUND;
PIPE ROW (emp_obj(l_emp_id, deptno, ename, job, sal));
END LOOP;
CLOSE l_cursor;
RETURN;
END;
Step 4:
Now, in the Page designer, create one region of type Interactive Report / Classic Report and under the source attribute select the type as SQL Query. Copy and paste the below sql query for calling the pipeline function for fetching the report data.
// Calling Pipeline Function SELECT * FROM TABLE(get_emp_pipe);


Comments
Post a Comment