Skip to main content
← PL/SQL Topics

Cursors & Bulk Operations

Explain returning a query result set to a caller with an OUT SYS_REFCURSOR

Returning Result Sets with SYS_REFCURSOR

ExplanationOracle Fusion / EBS · Technical · PL/SQLHigh confidenceGenerated

Open a SYS_REFCURSOR inside a procedure and hand it back so a client or another program can fetch the rows.

How it works

A weakly-typed SYS_REFCURSOR can be opened for any query and returned through an OUT parameter or a function result. The caller — a Java/ODP.NET client, a reporting tool, or another PL/SQL block — fetches and closes it. This is the standard way to expose a query from a package API without a pipelined function or a view. OPEN ... FOR can also take a dynamic string with USING binds.

How it works
CREATE OR REPLACE PROCEDURE get_dept_emps (
  p_dept_id IN  NUMBER,
  p_rows    OUT SYS_REFCURSOR
) AS
BEGIN
  OPEN p_rows FOR
    SELECT employee_id, last_name, salary
      FROM employees
     WHERE department_id = p_dept_id
     ORDER BY last_name;
END;
/
-- Caller (SQL*Plus): VARIABLE rc REFCURSOR;  EXEC get_dept_emps(10, :rc);  PRINT rc;

Related questions

GroundingGenerated

Model-generated, grounded against the curated knowledge layer. Check specifics against your instance.

Have a follow-up, or a different question?

Continue in Ask Oracle AI