FETCH Statement [ESQL] [SP]

Retrieves one row from the named cursor. The cursor must have been previously opened.

Quick Links:

Go to Parameters

Go to Examples

Go to Usage

Go to Standards

Go to Permissions

Syntax

FETCHNEXT | PRIOR | FIRST | LAST
   | ABSOLUTE row-count | RELATIVE row-count }
   ... cursor-name
   ... { [ INTO host-variable-list ]
   | USING DESCRIPTOR sqlda-name
   | INTO variable-list }
   ... [ PURGE ] [ BLOCK n ] [ ARRAY fetch-count ]
   ... INTO variable-list
   ... IQ CACHE row-count
These clauses are for use in Embedded SQL only:
  • USING DESCRIPTOR sqlda-name
  • INTO host-variable-list
  • PURGE
  • BLOCK n
  • ARRAY fetch-count
  • Use of host-variable in cursor-name and row-count

Parameters

(back to top)

Examples

(back to top)

Usage

(back to top)

One row from the result of SELECT is put into the variables in the variable list. The correspondence from the select list to the host variable list is one-to-one.

One or more rows from the result of SELECT are put either into the variables in the variable list or into the program data areas described by the named SQLDA. In either case, the correspondence from the select list to either the host variable list or the SQLDA descriptor array is one-to-one.

A cursor declared FOR READ ONLY sees the version of table(s) on which the cursor is declared when the cursor is opened, not the version of table(s) at the time of the first FETCH

If the FETCH includes a positioning parameter and the position is outside the allowable cursor positions, then the SQLE_NOTFOUND warning is issued.

DECLARE CURSOR must appear before FETCH in the C source code, and the OPEN statement must be executed before FETCH. If a host variable is being used for the cursor name, then the DECLARE statement actually generates code and thus must be executed before FETCH.

In the multiuser environment, rows can be fetched by the client more than one at a time. This is referred to as block fetching or multirow fetching. The first fetch causes several rows to be sent back from the server. The client buffers these rows and subsequent fetches are retrieved from these buffers without a new request to the server.

If the SQLSTATE_NOTFOUND warning is returned on the fetch, then the sqlerrd[2] field of the SQLCA (SQLCOUNT) contains the number of rows that the attempted fetch exceeded the allowable cursor positions. (A cursor can be on a row, before the first row or after the last row.) The value is 0 if the row was not found but the position is valid, for example, executing FETCH with a RELATIVE 1 clause when positioned on the last row of a cursor. The value is positive if the attempted fetch was further beyond the end of the cursor, and negative if the attempted fetch was further before the beginning of the cursor.

After successful execution of the FETCH statement, the sqlerrd[1] field of the SQLCA (SQLIOCOUNT) is incremented by the number of input/output operations required to perform the fetch. This field is actually incremented on every database statement.

The server returns in SQLCOUNT the number of records fetched and always returns a SQLCOUNT greater than zero unless there is an error. Older versions of the server only return a single row and the SQLCOUNT is set to zero. Thus a SQLCOUNT of zero with no error condition indicates one valid row has been fetched.

Standards

(back to top)

  • SQL—ISO/ANSI SQL compliant.
  • SAP Sybase Database product—Supported in Adaptive Server.

Permissions

(back to top)

The cursor must be opened and the user must have SELECT permission on the tables referenced in the declaration of the cursor.

Related reference
DECLARE CURSOR Statement [ESQL] [SP]
OPEN Statement [ESQL] [SP]
PREPARE Statement [ESQL]
CURSOR_WINDOW_ROWS Option