How cursors work

A cursor is a symbolic name associated with a select statement. It consists of the following parts:

The cursor position indicates the current row of the cursor. You can explicitly modify or delete that row using update or delete statements with a clause naming the cursor.

You can change the current cursor position through an operation called a fetch. The fetch command moves the current cursor position one or more rows down the cursor result set.

Figure 17-1 illustrates how the cursor result set and cursor position work when a fetch command is performed. In this example, the cursor is defined as follows:

declare cal_authors_crsr cursor
for select au_id, au_lname, au_fname
from authors
where state = "CA"
for update

Figure 18-1: How the cursor result set and cursor position work for a fetch

You might think of a cursor as a “handle” on the result set of a select statement. It enables you to examine and possibly manipulate one row at a time. However, cursors support only forward (or sequential) movement through the query results. Once you fetch several rows, you cannot backtrack through the cursor result set to access them again.