The RETURN_RESULT stored procedure is used to directly return the result set of a specified cursor to the client. The caller no longer needs to fetch data row by row using subprograms such as FETCH_ROWS and COLUMN_VALUE.
Note
For V4.4.2, the RETURN_RESULT subprogram is supported starting from V4.4.2.3 hotfix1.
Limitations and considerations
- This subprogram is supported only in Oracle-compatible mode.
cursor_numberrefers to the integer cursor ID returned by theDBMS_SQL.OPEN_CURSORsubprogram (registered in the session's DBMS_SQL cursor table), not a cursor variable declared in PL/SQL. For cursors of typeSYS_REFCURSOR, use the second overload.- Both parameters
cursor_numberandrcare of theIN OUTmode. Variables must be passed when calling them; literal values or other constants cannot be used directly. - The
to_clientparameter currently only supports the valueTRUE. When set toFALSE, an error indicating unsupported operation will be returned (error code-4007), with a message stating thatDBMS_SQL.RETURN_RESULT with to_client => FALSEis not supported. - Fixed the issue where an error
ORA-29471(DBMS_SQL access denied) was returned when an invalid cursor ID (not the cursor ID returned byDBMS_SQL.OPEN_CURSOR) was passed in. - When using the
cursor_numberoverload, the cursor must have completedPARSE(for aSELECTstatement) andEXECUTE, but the results have not all been fetched. Otherwise, an errorORA-01001(invalid cursor) is returned. - When using the
SYS_REFCURSORoverload, thercmust be anOPENcursor; otherwise, an errorORA-01001(invalid cursor) is reported. - You can call
RETURN_RESULTmultiple times within a stored procedure. Multiple result sets are returned to the client in the order they are called. - After the result set is sent, this subprogram closes the cursor's result set but does not release the cursor ID. Cursor IDs opened by using
DBMS_SQL.OPEN_CURSORmust still be released by callingDBMS_SQL.CLOSE_CURSOR.
Syntax
DBMS_SQL.RETURN_RESULT(
cursor_number IN OUT INTEGER,
to_client IN BOOLEAN DEFAULT TRUE);
DBMS_SQL.RETURN_RESULT(
rc IN OUT SYS_REFCURSOR,
to_client IN BOOLEAN DEFAULT TRUE);
Parameters
Parameter |
Note |
|---|---|
| cursor_number | ViaDBMS_SQL.OPEN_CURSOROpened and executedSELECTThe cursor ID of the statement. |
| rc | PassedOPEN ... FOROpenedSYS_REFCURSORVariable. |
| to_client | Whether to return the result set to the client. Default value:TRUE, currently only supportsTRUE. |
Examples
Example 1: Return a result set by using cursor_number
Create a test table and insert data into it.
obclient> CREATE TABLE return_result_tbl (id NUMBER, name VARCHAR2(20));
obclient> INSERT INTO return_result_tbl VALUES (1, 'one');
obclient> INSERT INTO return_result_tbl VALUES (2, 'two');
obclient> INSERT INTO return_result_tbl VALUES (3, 'three');
Create a stored procedure named return_result_proc and call RETURN_RESULT on the dynamic SQL cursor.
obclient> CREATE OR REPLACE PROCEDURE return_result_proc IS
cur_id INTEGER;
rows INTEGER;
BEGIN
cur_id := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(cur_id, 'SELECT id, name FROM return_result_tbl ORDER BY id', DBMS_SQL.NATIVE);
rows := DBMS_SQL.EXECUTE(cur_id);
DBMS_SQL.RETURN_RESULT(cur_id);
END;
/
When a stored procedure is called, the client directly receives the result set.
obclient> CALL return_result_proc();
The return result is as follows:
+------+-------+
| ID | NAME |
+------+-------+
| 1 | one |
| 2 | two |
| 3 | three |
+------+-------+
3 rows in set
Example 2: Return a result set by using SYS_REFCURSOR
Create the stored procedure return_result_ref_proc to return a result set using SYS_REFCURSOR.
obclient> CREATE OR REPLACE PROCEDURE return_result_ref_proc IS
rc SYS_REFCURSOR;
BEGIN
OPEN rc FOR SELECT id FROM return_result_tbl WHERE id = 1;
DBMS_SQL.RETURN_RESULT(rc);
END;
/
Call the stored procedure.
obclient> CALL return_result_ref_proc();
The client receives the following result set:
+------+
| ID |
+------+
| 1 |
+------+
1 row in set
You can call RETURN_RESULT multiple times within the same stored procedure. The result sets are returned to the client in the order they are called.