PL/SQL - Cursor

> Procedural Languages > PL/SQL - (Procedure Language|PL) SQL

1 - About

A cursor in the context of Oracle.

A cursor is a SQL datatype.

A cursor is a pointer to a private SQL area that stores information about processing a specific:

  • or DML statement.

The cursor data are session-specific and therefore are stored in the private SQL area. A cursor contains:

Oracle does not return Result Sets but a pointer. A cursor can be passed between:

  • the RDBMS and the client,
  • or between PL/SQL and Java in the database.

It can also be returned from a query or a stored procedure.


3 - Child

The result of a SQL statement is a cursor.

When the same SQL text statement (the parent) is executed several time in different conditions, you will get several child cursors.

The parent cursor stores the sql text of the cursor.

different conditions can be caused by:

  • different bind variable
  • session parameter (CURSOR_SHARING,optimizer_mode, …)

Different conditions leads to a different execution plan that leads to different child cursors.

4 - Property

Cursor variable parameters are pointers. Therefore, if a subprogram assigns one cursor variable parameter to another, they refer to the same memory location. (Aliasing in Oracle term).

5 - Management

5.1 - Initialization

A cursor is not a PL/SQL variable but a SQL one, you cannot therefore assign values to a cursor.

To access data returned by a query, you define CURSORS and REF CURSORS where:

Unlike an explicit cursor, which always refers to the same query work area, a cursor variable can refer to different work areas. You cannot use a cursor variable where a cursor is expected, or vice versa.


5.1.1 - Parameter

A cursor can take IN parameters (the constraint NOT NULL can not be imposed)

              SELECT * FROM departments WHERE department_id > low
              AND department_id < high;

5.2 - Control

You use three commands to control a cursor:

  • OPEN,
  • FETCH,
  • and CLOSE

5.2.1 - Open

Opening the cursor executes the query and retrieve the result set (all rows that meet the query) and set the pointer before the first row. The FETCH statement will retrieve the first row.

For cursors declared using the FOR UPDATE clause, the OPEN statement also locks those rows..

   CURSOR c1 IS SELECT employee_id, last_name, job_id, salary FROM employees
      WHERE salary > 2000; 
  OPEN C1;

5.2.2 - Fetch

You can:

  • execute FETCH repeatedly until all rows have been retrieved (A FETCH statement retrieve the data of the next row)
  • or use the BULK COLLECT clause to fetch all rows at once.

5.2.3 - Close

The cursor resources are released with a CLOSE statement.

5.3 - Attribute

A cursor attribute can be appended to the name of a cursor or cursor variable.

  • %FOUND,
  • %NOTFOUND, Before the first fetch from an open cursor, cursor_name%NOTFOUND returns NULL. Thereafter, it returns FALSE if the last fetch returned a row, or TRUE if the last fetch failed to return a row.
  • %ISOPEN,
  • %ROWCOUNT. When a cursor is opened, %ROWCOUNT is zeroed. Before the first fetch, cursor_name%ROWCOUNT returns always 0. Thereafter, it returns the number of rows fetched so far. The number is incremented if the latest fetch returned a row.

5.4 - Metadata Information


6 - Documentation / Reference