Previous |
Next |
A cursor variable is like a cursor (see "About Cursors"), except that it is not limited to one query. You can open a cursor variable for a query, process the result set, and then use the cursor variable for another query. Cursor variables are useful for passing query results between subprograms.
To declare a cursor variable, you declare a REF
CURSOR
type, and then declare a variable of that type (therefore, a cursor variable is often called a REF
CURSOR
). A REF
CURSOR
type can be either strong or weak.
A strong REF
CURSOR
type specifies a return type, which is the RECORD
type of its cursor variables. The PL/SQL compiler does not allow you to use these strongly typed cursor variables for queries that return rows that are not of the return type. Strong REF
CURSOR
types are less error-prone than weak ones, but weak ones are more flexible.
A weak REF
CURSOR
type does not specify a return type. The PL/SQL compiler accepts weakly typed cursor variables in any queries. Weak REF
CURSOR
types are interchangeable; therefore, instead of creating weak REF
CURSOR
types, you can use the predefined type weak cursor type SYS_REFCURSOR
.
After declaring a cursor variable, you must open it for a specific query (with the OPEN
FOR
statement), fetch rows one at a time from the result set (with the FETCH
statement), and then either close the cursor (with the CLOSE
statement) or open it for another specific query (with the OPEN
FOR
statement). Opening the cursor variable for another query closes it for the previous query. After closing a cursor variable for a specific query, you can neither fetch records from the result set of that query nor see the cursor attribute values for that query.
Oracle Database PL/SQL Language Reference for more information about using cursor variables
Oracle Database PL/SQL Language Reference for the syntax of cursor variable declaration