Sunday, July 08, 2007

Cursors: Key Points

Cursors
A cursor is a name for a specific private SQL area in which information for processing
the specific statement is kept. PL/SQL uses both implicit and explicit cursors. PL/SQL
implicitly declares a cursor for all SQL data manipulation statements on a set of rows,
including queries that return only one row. For queries that return more than one row,
you can explicitly declare a cursor to process the rows individually.
Cursor attribute for implicit cursor
Sql%found : Until a SQL data manipulation statement is executed, %FOUND yields NULL. Thereafter,
%FOUND yields TRUE if an INSERT, UPDATE, or DELETE statement affected one or
more rows, or a SELECT INTO statement returned one or more rows.

%ISOPEN : Oracle closes the SQL cursor automatically after executing its associated SQL
statement. As a result, %ISOPEN always yields FALSE.
%ROWCOUNT Attribute: How Many Rows Affected So Far

If a SELECT INTO statement returns more than one row, PL/SQL raises the predefined
exception TOO_MANY_ROWS and %ROWCOUNT yields 1, not the actual number of rows
that satisfy the query
The SQL%ROWCOUNT attribute is not related to the state of a transaction. When a
rollback to a savepoint is performed, the value of SQL%ROWCOUNT is not restored to the
old value before the savepoint was taken. Also, when an autonomous transaction is
exited, SQL%ROWCOUNT is not restored to the original value in the parent transaction.
It might be in a different scope

The %NOTFOUND attribute is not useful in combination with the SELECT INTO
statement:
– If a SELECT INTO statement fails to return a row, PL/SQL raises the
predefined exception NO_DATA_FOUND immediately, interrupting the flow of
control before you can check %NOTFOUND.
– A SELECT INTO statement that calls a SQL aggregate function always returns
a value or a null. After such a statement, the %NOTFOUND attribute is always
FALSE, so checking it is unnecessary.

The value of the SQL%ROWCOUNT attribute refers to the most recently executed SQL

No comments: