How cursors work  Using cursors

Chapter 18: Cursors: Accessing Data

Sensitivity and scrollability

Adaptive Server offers two keywords to specify sensitivity:

If you declare a cursor insensitive, the cursor shows only the result set as it is when the cursor is opened; data changes in the underlying tables are not visible. If you declare it semi_sensitive, some changes in the base tables made since opening the cursor may appear in the result set. Data changes may or may not be visible to the semi-sensitive cursor.

If you specify neither attribute, the default value is semi_sensitive.

Adaptive server also provides keywords for specifying scrollability:

If you declare a cursor using scroll, it will be scrollable, which means that you can fetch the result rows sequentially or non-sequentially, and you can scan the result set repeatedly. If the option no scroll appears in the cursor declaration, the cursor is non-scrollable; the result set appears in a forward-only direction, one row at a time.

If you specify neither attribute, the default value is no scroll.When you specify neither sensitivity nor scrollability, the cursor is a default cursor: a semi-sensitive, non-scrollable cursor.

You can think of a cursor as a “handle” on the result set of a select statement. The cursor can be fetched either sequentially or non-sequentially, depending on the cursor’s scrollability.

The non-scrollable, or forward-only cursor, can be fetched only in a forward direction; you cannot go back to a row that is already fetched. The scrollable cursor can be fetched in either direction, backwards or forwards.

A scrollable cursor allows you to set the position of the cursor anywhere in the cursor result set as long as the cursor is open, by specifying the option first, last, absolute, next, prior, or relative in a fetch statement.

To fetch the last row in a result set, enter:

fetch last [from] <cursor_name>

Or, to select a specific row in the result set, in this case the 500th row, enter:

fetch absolute 500 [from] <cursor_name>

All scrollable cursors are read-only. Any cursor that can be updated is non-scrollable.





Copyright © 2005. Sybase Inc. All rights reserved. Using cursors

View this book as PDF