SqlCursorFetch%

Fetches a block of rows (called the fetch buffer) from a client cursor or transparent server cursor, and makes the rows available using SqlCursorData$. If you are connected to SQL Server 6.0, you should use SqlCursorFetchEx%.

Syntax

SqlCursorFetch% ( cursorhandle%, fetchtype%, rownum% )

where

cursorhandle%
Is the cursor handle created by SqlCursorOpen%.
fetchtype%
Specifies the type of fetch to execute, changing the position of the fetch buffer within the cursor results set. The following table describes the different fetchtype% values:
fetchtype% Description
FETCHFIRST% Fetches the first block of rows from a dynamic or keyset cursor. The first row of the new fetch buffer is the first row in the cursor results set.
FETCHNEXT% Fetches the next block of rows from a dynamic or keyset cursor. The first row of the new fetch buffer is the row after the last row of the current fetch buffer.

If this is the first fetch using a new cursor, it behaves the same as FETCHFIRST%.

FETCHPREV% Fetches the previous block of rows from a fully dynamic or keyset cursor. The first row of the new fetch buffer is nrows% (specified in SqlCursorOpen%) before the first row of the current fetch buffer.
FETCHRANDOM% Fetches a block of rows from a keyset cursor. The first row of the new fetch buffer is the specified rownum% row in the keyset cursor results set.
FETCHRELATIVE% Fetches a block of rows from a keyset cursor. The first row of the new fetch buffer is rownum% rows before or after the first row of the current fetch buffer.
FETCHLAST% Fetches the last block of rows from a keyset cursor. The last row of the new fetch buffer is the last row of the cursor results set.

The block of rows retrieved by a fetch is called the fetch buffer. The number of rows in the fetch buffer is determined by the nrows% parameter of SqlCursorOpen%.

For a forward-only dynamic cursor (scrollopt% is CURFORWARD% in SqlCursorOpen%), you can use only FETCHFIRST% or FETCHNEXT%.

rownum%
Is the specified random or relative row number to use as the first row of the new fetch buffer. Use this parameter only with a fetchtype% of FETCHRANDOM% or FETCHRELATIVE%. Specify 0 for any other fetchtype%.

When fetchtype% is FETCHRANDOM%, the first row of the new fetch buffer is the rownum% row (counting forward from the beginning) of the keyset cursor results set. The rownum% parameter must be positive.

When fetchtype% is FETCHRELATIVE%:

Returns

SUCCEED (1) or FAIL (0).

SUCCEED (1) is returned if every row was fetched successfully. Note that for a keyset cursor, a fetch that results in a missing row will not cause SqlCursorFetch% to FAIL (0).

FAIL (0) is returned if at least one of the following is true:

Remarks

Specify the size of the fetch buffer in the nrows% parameter of SqlCursorOpen%.

After the fetch, the elements of the array of row status indicators (pstatus&() in SqlCursorOpen%) are filled with row status values, one for each row in the fetch buffer. Each row status value is a series of fetch status values ORed together. The following table shows the meaning of each row status value:

Fetch status Description
FTCSUCCEED% The row was successfully fetched. SqlCursorData$ will return valid data for the row.
FTCMISSING% The row has been deleted or a unique index column of the row has been changed. Do not use SqlCursorData$ for the row.

For keyset cursors, this fetch status can appear at any time. For dynamic cursors, this fetch status can appear only after the current fetch buffer is refreshed.

FTCENDOFKEYSET% Is the end of the keyset. This fetch status is set for backward compatibility with "mixed" client cursors used by existing applications.
FTCENDOFRESULTS% Is the end of the results set of a dynamic or keyset cursor. Rows in the fetch buffer after this row are invalid and will have a row status indicator of 0; do not use SqlCursorData$ for those rows.

If ORed with FTCSUCCEED%, this is the last row in the cursor results set; it contains valid data.

If ORed with FTCMISSING%, this is the last row in the cursor results set, but the row is missing.

If not ORed with FTCSUCCEED% or FTCMISSING%, this row is invalid.


A row status indicator of 0 means that the row is invalid, and SqlCursorData$ will not return valid data. This usually happens when the row is before the beginning (first row) or after the end (last row) of the cursor results set.

After the fetch, SqlCursorData$ returns:

If no fetches have been performed on a cursor, the current cursor position is before the beginning (first row) of the cursor results set.

After a fetch is complete, the new cursor position is one of the following:

When the new cursor position is unchanged because the first row (and thus all rows) of the new fetch buffer would have been after the last row of the cursor results set, all rows in the fetch buffer are invalid and will not have a fetch status of FTCSUCCEED%. In the case of dynamic cursors, the first row of the fetch buffer will have a fetch status of FTCENDOFRESULTS%, and later rows will have a row status of 0. In the case of keyset cursors, all rows in the fetch buffer will have a row status of 0.

When the new position of a dynamic cursor is unchanged because all rows of the new fetch buffer would have been before the first row of the cursor results set, all rows in the fetch buffer are invalid and will not have a fetch status of FTCSUCCEED%. The first row of the fetch buffer will have a fetch status of FTCENDOFRESULTS%, and later rows will have a row status of 0.

When the new cursor position is unchanged and all rows in the fetch buffer are invalid, you can use SqlCursor% to refresh the rows in the fetch buffer with current data from SQL Server. This will result in valid rows that reflect the current cursor position.

Each call to SqlCursorFetch% leaves the connection available for use with no pending results.

Note This function works with client cursors and transparent server cursors. Do not use both SqlCursorFetch% and SqlCursorFetchEx% with the same cursor handle. Once one of these functions is used on a specific cursor handle, any attempt to use the other function will return fail (0).

If rows in the current fetch buffer of a dynamic cursor are deleted, a fetch using a client cursor might behave differently than a fetch using a transparent server cursor.

Client cursor:

When the new position of a dynamic cursor is adjusted to be the first row of the cursor results set (which happens when the first row of the new fetch buffer would have been before the first row of the dynamic cursor results set and the last row of the new fetch buffer would have stayed within the dynamic cursor results set due to a FETCHPREV% operation), some rows at the end of the new fetch buffer might be invalid. Any invalid rows will have a row status indicator of 0.

If rows in the current fetch buffer of a dynamic cursor are deleted, a fetch next or fetch previous might result in a new fetch buffer that skips rows in the cursor results set or includes rows from the current fetch buffer again.

Transparent server cursor:

A fetchtype% of FETCHNEXT% or FETCHPREV% using a dynamic cursor is actually mapped to a relative fetch on SQL Server 6.0. Because of this, if the first row in the current fetch buffer is deleted before a FETCHNEXT% (mapped to a forward relative fetch on SQL Server 6.0) is performed, the current cursor position becomes invalid. For more information about the fetch behavior in this case, see SqlCursorFetchEx%.

See Also

SqlCursor%, SqlCursorColInfo%, SqlCursorClose, SqlCursorInfo%, SqlCursorOpen%