Tuesday, May 11, 2010

Cursor attributes

Cursor attributes are variables that take some value about the status of the cursor. These values are automatically set by Oracle and the programmer can read them not write values for them. There are four cursor attributes. They are




 %FOUND

 %ISOPEN

 %NOTFOUND

 %ROWCOUNT



 %FOUND:

After a cursor is opened before the first fetch, the value of this variable is null. After the first fetch, if the query returns one or more rows as result set, this variable is set to TRUE. When a fetch is made after the last row of the result set is reached, this variable is set to FALSE.

This variable is extensively used to in stored procedures to handle exceptions when a query returns no data set. If this variable is referenced before the cursor is opened, an exception INVALID_CURSOR is raised.

 %ISOPEN

This variable is set to TRUE if a cursor is opened and false when the cursor is closed.

 %NOTFOUND

This variable is a logical opposite of %FOUND. This variable is set to TRUE if the last fetch returns no rows an FALSE when the last fetch returns a row. This can also be used in exception handing when a query returns no rows.

 %ROWCOUNT

This variable acts like a counter. It is set to zero when a cursor is opened. Thereafter, with each fetch, the value of this variable is incremented by 1 if the fetch returns a row. This variable is handy when processing needs to be done for only a few rows of the result set.