1.9 Cursors in PL/SQL

Every SQL statement executed by the RDBMS has a private SQL area that contains information about the SQL statement and the set of data returned. In PL/SQL, a cursor is a name assigned to a specific private SQL area for a specific SQL statement. There can be either static cursors, whose SQL statement is determined at compile time, or dynamic cursors, whose SQL statement is determined at runtime. Static cursors are used only for DML statements (SELECT, INSERT, UPDATE, DELETE, MERGE, or SELECT FOR UPDATE). These static cursors can be explicitly declared and named or may appear in-line as an implicit cursor. Dynamic cursors are used for any type of valid SQL statement including DDL (CREATE, TRUNCATE, ALTER) and DCL (GRANT, REVOKE). Dynamic cursors are implemented with the EXECUTE IMMEDIATE statement.

1.9.1 Explicit Cursors

Explicit cursors are SELECT statements that are DECLAREd explicitly in the declaration section of the current block or in a package specification. Use OPEN, FETCH, and CLOSE in the execution or exception sections of your programs. Declaring explicit cursors

To use an explicit cursor, you must first declare it in the declaration section of a block or package. There are three types of explicit cursor declarations:

  • A cursor without parameters; for example:

    CURSOR company_cur 
       SELECT company_id FROM company;
  • A cursor that accepts arguments through a parameter list; for example:

    CURSOR company_cur (id_in IN NUMBER) IS
    SELECT name FROM company
    WHERE  company_id = id_in;
  • A cursor header that contains a RETURN clause in place of the SELECT statement; for example:

    CURSOR company_cur (id_in IN NUMBER) 
    RETURN company%ROWTYPE;

This last example shows that the cursor can be declared separately from its implementation; for example, the header in a package specification and the implementation in the package body. See Section 1.14 for more information. Opening explicit cursors

To open a cursor, use the following syntax:

OPEN cursor_name [(argument [,argument ...])];

where cursor_name is the name of the cursor as declared in the declaration section. The arguments are required if the definition of the cursor contains a parameter list.

You must open an explicit cursor before you can fetch rows from that cursor. When the cursor is opened, the processing actually includes the parse, bind, open, and execute phases of SQL statement execution. This OPEN processing includes determining an execution plan, associating host variables and cursor parameters with the placeholders in the SQL statement, determining the result set, and, finally, setting the current row pointer to the first row in the result set.

When using a cursor FOR loop, the OPEN is implicit in the FOR statement. If you try to open a cursor that is already open, PL/SQL will raise an "ORA-06511: PL/SQL: cursor already open" exception. Fetching from explicit cursors

The FETCH statement places the contents of the current row into local variables. To retrieve all rows in a result set, each row needs to be fetched. The syntax for a FETCH statement is:

FETCH cursor_name INTO record_or_variable_list;

where cursor_name is the name of the cursor as declared and opened. Closing explicit cursors

After all rows have been fetched, a cursor needs to be closed. Closing a cursor enables the PL/SQL memory optimization algorithm to release the associated memory at an appropriate time. You can close an explicit cursor by specifying a CLOSE statement as follows:

CLOSE cursor_name;

where cursor_name is the name of the cursor declared and opened.

If you declare a cursor in a local anonymous, procedure, or function block, that cursor will automatically close when the block terminates. Package-based cursors must be closed explicitly, or they stay open for the duration of your session. Closing a cursor that is not open raises an INVALID CURSOR exception. Explicit cursor attributes

There are four attributes associated with cursors: ISOPEN, FOUND, NOTFOUND, and ROWCOUNT. These attributes can be accessed with the % delimiter to obtain information about the state of the cursor. The syntax for a cursor attribute is:


where cursor_name is the name of the explicit cursor.

The behaviors of the explicit cursor attributes are described in the following table:




TRUE if cursor is open.

FALSE if cursor is not open.


INVALID_CURSOR is raised if cursor has not been OPENed.

NULL before the first fetch.

TRUE if record was fetched successfully.

FALSE if no row was returned.

INVALID_CURSOR if cursor has been CLOSEd.


INVALID_CURSOR is raised if cursor has not been OPENed.

NULL before the first fetch.

FALSE if record was fetched successfully.

TRUE if no row was returned.

INVALID_CURSOR if cursor has been CLOSEd.


INVALID_CURSOR is raised if cursor has not been OPENed.

The number of rows fetched from the cursor.

INVALID_CURSOR if cursor has been CLOSEd.

Frequently, a cursor attribute is checked as part of a WHILE loop that fetches rows from a cursor, as shown here:

   caller_rec caller_pkg.caller_cur%ROWTYPE;
   OPEN caller_pkg.caller_cur;
      FETCH caller_pkg.caller_cur into caller_rec;
      EXIT WHEN caller_pkg.caller_cur%NOTFOUND
               caller_pkg.caller_cur%ROWCOUNT > 10;

      UPDATE call
         SET caller_id = caller_rec.caller_id
         WHERE call_timestamp < SYSDATE;
   CLOSE caller_pkg.caller_cur;

1.9.2 Implicit Cursors

Whenever a SQL statement is directly in the execution or exception section of a PL/SQL block, you are working with implicit cursors. SQL statements handled this way include INSERT, UPDATE, DELETE, MERGE, and SELECT INTO. Unlike explicit cursors, implicit cursors do not need to be declared, OPENed, FETCHed, or CLOSEd.

SELECT statements handle the %FOUND and %NOTFOUND attributes differently from the way that explicit cursors do. When an implicit SELECT statement does not return any rows, PL/SQL immediately raises the NO_DATA_FOUND exception and control passes to the exception section. When an implicit SELECT returns more than one row, PL/SQL immediately raises the TOO_MANY_ROWS exception and control passes to the exception section.

Implicit cursor attributes are referenced via the SQL cursor. For example:

   UPDATE activity SET last_accessed := SYSDATE
   WHERE UID = user_id;

      INSERT INTO activity_log (uid,last_accessed)
      VALUES (user_id,SYSDATE);

The following table lists the implicit cursor attributes:




Always FALSE because the cursor is opened implicitly and closed immediately after the statement is executed.


NULL before the statement.

TRUE if one or more rows were inserted, merged, updated, or deleted or if only one row was selected.

FALSE if no row was selected, merged, updated, inserted, or deleted.


NULL before the statement.

TRUE if no row was selected, merged, updated, inserted, or deleted.

FALSE if one or more rows were inserted, merged, updated, or deleted.


Number of rows affected by the cursor.


Pseudo index-by table containing the numbers of rows affected by the statements executed in bulk bind operations. See Section 1.17.8 for more information. (Introduced with Oracle8i.)

Use the RETURNING clause in INSERT, UPDATE, and DELETE statements to obtain data modified by the associated DML statement. This clause allows you to avoid an additional SELECT statement to query the results of the DML statement. For example:

   UPDATE activity SET last_accessed := SYSDATE
   WHERE UID = user_id
   RETURNING last_accessed, cost_center 
   INTO timestamp, chargeback_acct; SELECT FOR UPDATE clause

By default, the Oracle RDBMS locks rows as they are changed. To lock all rows in a result set, use the FOR UPDATE clause in your SELECT statement when you OPEN the cursor, instead of when you change the data. Using the FOR UPDATE clause does not require you to actually make changes to the data; it only locks the rows when opening the cursor. These locks are released on the next COMMIT or ROLLBACK. As always, these row locks do not affect other SELECT statements unless they, too, are FOR UPDATE. The FOR UPDATE clause is appended to the end of the SELECT statement and has the following syntax:

  FROM ...
   FOR UPDATE [OF column_reference] [NOWAIT];

where column_reference is a comma-delimited list of columns that appear in the SELECT clause. The NOWAIT keyword tells the RDBMS to not wait for other blocking locks to be released. The default is to wait forever.

In the following example, only columns from the inventory (pet) table are referenced FOR UPDATE, so no rows in the dog_breeds (dog) table are locked when hounds_in_stock_cur is opened:

   CURSOR hounds_in_stock_cur IS
      SELECT pet.stock_no, pet.breeder, dog.size
        FROM dog_breeds dog ,inventory pet
       WHERE dog.breed = pet.breed
         AND dog.class = 'HOUND'
         FOR UPDATE OF pet.stock_no, pet.breeder;

UPDATE and DELETE statements can use a WHERE CURRENT OF clause if they reference a cursor declared FOR UPDATE. This syntax indicates that the UPDATE or DELETE should modify the current row identified by the FOR UPDATE cursor. The syntax is:

   WHERE CURRENT OF cursor_name;

By using WHERE CURRENT OF, you do not have to repeat the WHERE clause in the SELECT statement. For example:

   CURSOR wip_cur IS
      SELECT acct_no, enter_date FROM wip
       WHERE enter_date < SYSDATE - 7
         FOR UPDATE;
   FOR wip_rec IN wip_cur
      INSERT INTO acct_log (acct_no, order_date)
         VALUES (wip_rec.acct_no, wip_rec.enter_
      DELETE FROM wip 
         WHERE CURRENT OF wip_cur;

1.9.3 Dynamic Cursors

Dynamic cursors are implemented with an EXECUTE IMMEDIATE statement together with the OPEN FOR, FETCH, and CLOSE statements. The EXECUTE IMMEDIATE statement supports single-row queries and DDL, while the OPEN FOR, FETCH, and CLOSE statements support dynamic multi-row queries. The syntax for these statements is:

  [INTO {variable [,variable ...] | record}] 
  [USING [IN | OUT | IN OUT] bind_argument
    [,[IN | OUT | IN OUT] bind_argument ...] ]
  [{RETURNING | RETURN} INTO bind_argument   [,bind_argument]...];

The EXECUTE IMMEDIATE statement parses and executes the SQL statement in a single step. The EXECUTE IMMEDIATE statement requires a terminating semicolon, but the sql_statement must not have a trailing semicolon. For example:

   ' TO ' || grantee_list;

The OPEN FOR statement assigns a multi-row query to a weakly typed cursor variable. The rows are then FETCHed and the cursor CLOSEd:

   cv cv_typ;
   laccount_no NUMBER;
   lbalance NUMBER;
   OPEN cv FOR
      'SELECT account_no, balance
         FROM accounts
        WHERE balance < 500';
      FETCH cv INTO laccount_no, lbalance;
      -- Process the row.
   CLOSE cv;

Because SQL statements usually execute repeatedly, declare your dynamic cursor with bind variables and pass the values to Oracle at runtime. The parsed form of the statement can be reused from the shared pool, improving performance. For example:

  (region_id, region_name) VALUES (:r_id, :r_name)'
  USING  id, name; 

1.9.4 Cursor Variables

A cursor variable is a data structure that points to a cursor object, which in turn points to the cursor's result set. You can use cursor variables to more easily retrieve rows in a result set from client and server programs. You can also use cursor variables to hide minor variations in queries.

The syntax for a REF_CURSOR type (cursor variable) is:

TYPE ref_cursor_name IS REF CURSOR 
   [RETURN record_type];

If you do not include a RETURN clause, then you are declaring a weak REF CURSOR. Cursor variables declared from weak REF CURSORs can be associated with any query at runtime. A REF CURSOR declaration with a RETURN clause defines a "strong" REF CURSOR. A cursor variable based on a strong REF CURSOR can be associated with queries whose result sets match the number and datatype of the record structure after the RETURN at runtime.

To use cursor variables, you must first create a REF_CURSOR type, then declare a cursor variable based on that type.

The following example shows the use of both weak and strong REF CURSORs:

   -- Create a cursor type based on the company's   
   TYPE company_curtype IS REF CURSOR 
      RETURN companies%ROWTYPE;

   -- Create the variable based on the REF CURSOR.
   company_cur company_curtype;

   -- And now the weak, general approach.
   TYPE any_curtype IS REF CURSOR;
   generic_curvar any_curtype;

The syntax to OPEN a cursor variable is:

OPEN cursor_name FOR SELECT_statement;

FETCH and CLOSE a cursor variable using the same syntax as for explicit cursors. There are a number of restrictions on cursor variables:

  • You cannot declare package-level cursor variables because they do not have a persistent state. (You can declare them in packaged procedures and functions, however.)

  • You cannot assign NULLs to a cursor variable nor can you use comparison operators to test for equality, inequality, or nullity.

  • Neither database columns nor collections can store cursor variables.

  • You cannot use RPCs to pass cursor variables from one server to another.

1.9.5 Cursor Expressions

A cursor expression is a cursor that is used as a column expression in the SELECT list of an explicit cursor. The syntax for a cursor expression is:

CURSOR (subquery)

Cursor expressions can reduce the amount of redundant data returned to a calling program over techniques that involve joining the tables together. The cursor expression is automatically opened when the parent row is fetched. Cursor expressions can be nested as well. These nested cursors are closed when one of the following occurs:

  • The nested cursor is explicitly closed by the program.

  • The parent cursor is closed.

  • The parent cursor is re-executed.

  • An exception is raised during the fetch of the parent row.

An example of a cursor expression follows:

CURSOR order_cur IS
   SELECT o.order_date ,o.order_status
         ,CURSOR(SELECT p.translated_name
                 FROM oe.order_items i
                     ,oe.product_descriptions p
                 WHERE i.product_id = p.product_id
                  AND  i.order_id = o.order_id)
   FROM oe.orders o
   WHERE order_date BETWEEN TO_DATE('01-Jan-03')
                        AND TO_DATE('31-Jan_03');
odate   oe.orders.order_date%TYPE;
ostatus oe.orders.order_status%TYPE;
od_cur  refcursor;
tname   oe.product_descriptions.translated_name%TYPE;
price   oe.order_items.unit_price%TYPE;
qty     oe.order_items.quantity%TYPE;
   OPEN order_cur;
      FETCH order_cur INTO odate, ostatus, od_cur;
      EXIT WHEN order_cur%NOTFOUND;
         FETCH od_cur INTO tname, price, qty;
         EXIT WHEN od_cur%NOTFOUND;
      END LOOP;
   CLOSE order_cur;