A cursor is a name attached to a database result set in memory.
You might think of it as a pointer or reference to a virtual table containing
zero or more rows of data. Because PL/SQL is a language that is used
primarily within a database environment, the use of cursors is unavoidable for
all but the simplest of tasks. You have already seen cursors introduced in
earlier lessons. To review some of the major points:
- Cursors are explicit if they are specifically declared as such in
PL/SQL code. They are implicit when simply used in the background as
part of a SELECT INTO or DML statement.
- The basic pattern for dealing with explicit cursors is to declare
the cursor, open it, loop through the rows referenced by the
cursor (if necessary), fetch the results into variables and
close the cursor.
- An alternative to this is to use a cursor for loop where a cursor
is created on the fly without the need to declare, open or close it.
- Variables declared with %TYPE anchored declarations are based upon corresponding
database fields. Variables declared with %ROWTYPE anchored declarations are based upon
entire rows in a table.
- Cursor attributes (e.g. %NOTFOUND, %FOUND, %ISOPEN, %ROWCOUNT) provide
information about the cursor in use.