Various relational databases enable stored procedures to return result sets (one or more sets of rows).
Traditionally, database stored procedures worked exactly like procedures in any high-level programming language. They had a fixed number of arguments which could be of types IN
, OUT
, or IN OUT
. If a procedure had n
arguments, it could return at most n
values as results. However, suppose that you wanted a stored procedure to execute a query such as SELECT * FROM emp
and return the results. The emp
table might have a fixed number of columns, but there is no way of telling, at procedure creation time, the number of rows it has. Because of this, no traditional stored procedure could be created that returned the results of this type of query. As a result, several relational database vendors added the ability to return results sets from stored procedures, but each relational database returns result sets from stored procedures differently.
Oracle has a data type called a REF CURSOR
. Like every other Oracle data type, a stored procedure can take this data type as an IN
or OUT
argument. With Oracle Database, a stored procedure must have an output argument of type REF CURSOR
. It then opens a cursor for a SQL statement and places a handle to that cursor in that output parameter. The caller can then retrieve from the REF CURSOR
the same way as from any other cursor.
Oracle Database can do a lot more than return result sets. The REF CURSOR
data type can be passed as an input argument to PL/SQL routines to be passed back and forth between client programs and PL/SQL routines or as an input argument between several PL/SQL routines.