OCILIB Tutorials v5.0.0
IntermediateOracle ≥ 12.1demo/implicit_resultset.cdemo/implicit_resultset.cpp

Implicit resultsets (12c+)

Objectives — by the end of this page you will be able to:

Concepts

Since Oracle 12c, a PL/SQL block can return result sets without declaring them as parameters — DBMS_SQL.RETURN_RESULT pushes cursors the client picks up implicitly:

declare
    c1 sys_refcursor;
    c2 sys_refcursor;
begin
    open c1 for select ename from employees;
    dbms_sql.return_result(c1);
    open c2 for select dname from departments;
    dbms_sql.return_result(c2);
end;

After the block executes, the statement's resultset is the first pushed cursor; asking for the next resultset advances through the queue until none remain:

OCI_StatementExecute(st, OTEXT(
    "declare "
    "  c1 sys_refcursor; "
    "  c2 sys_refcursor; "
    "begin "
    "  open c1 for select ename from employees where deptno = 20; "
    "  dbms_sql.return_result(c1); "
    "  open c2 for select dname from departments; "
    "  dbms_sql.return_result(c2); "
    "end;"));

OCI_Resultset *rs = OCI_StatementGetResultset(st);
while (rs)
{
    while (OCI_ResultsetFetchNext(rs))
    {
        printf("%s\n", OCI_ResultsetGetString(rs, 0));
    }
    printf("---\n");
    rs = OCI_StatementGetNextResultset(st);            /* NULL when no more */
}

Implicit vs REF CURSOR param

Implicit (RETURN_RESULT) sys_refcursor OUT
PL/SQL signature none needed out sys_refcursor param
Client call GetResultset + GetNextResultset bind-a-statement (Bind/BindStatement)
Multiple sets natural — a queue of cursors one per OUT param
Server version ≥ 12c any

Choose implicit for "return N lists" and 12c+ deployments; ref cursors when the procedure signature must be explicit or for older servers.

Putting it together — the typical sequence

Implicit resultsets are the no-cursor-declaration alternative:

  1. Execute the PL/SQL — a block that does DBMS_SQL.RETURN_RESULT or returns implicit cursors.
  2. Walk the resultsets — OCI_StatementGetNextResultset iterates the implicit cursors the call produced, one after another.
  3. Fetch each — same FetchNext + Get* loop, repeated per resultset.

Hints: implicit resultsets arrive in the order the PL/SQL returned them; GetNextResultset returns NULL when they're exhausted — that's the loop condition; and unlike ref cursors there's no bind — the resultsets appear on the statement after Execute, zero declaration needed.

Try it yourself

  1. Return 3 implicit resultsets and print the count you consumed.
  2. Mix return_result with an OUT scalar bind in the same block.

Common pitfalls

Recap

Next: index-by table parameters — arrays into and out of PL/SQL.