Icon FETCH

Navigates a result set cursor and reads column values into variables, parameters, or trigger NEWROW values.

Syntax
FETCH [<Orientation>] [FROM] <CursorName>
[[(<ColumnName> [,<ColumnName>])]
INTO <TargetName> [,<TargetName>]]

<Orientation> =

NEXT|PRIOR|FIRST|LAST|{ABSOLUTE|RELATIVE} <IntegerValue>

<TargetName> =

<VariableName>|<ParameterName>|NEWROW.<ColumnName>

Usage
Use this statement to navigate a result set cursor and read column values into variables, parameters, or NEWROW values in a trigger.

The various orientations work as follows:

OrientationDescription
NEXTNavigates to the next row in the cursor. If there are no more subsequent rows in the cursor, then the EOF flag is set for the cursor and the current row stays the same. If there are no rows in the cursor, then both the BOF and EOF flags are set for the cursor.
PRIORNavigates to the prior row in the cursor. If there are no more prior rows in the cursor, then the BOF flag is set for the cursor and the current row stays the same. If there are no rows in the cursor, then both the BOF and EOF flags are set for the cursor.
FIRSTNavigates to the first row in the cursor. If there are no rows in the cursor, then both the BOF and EOF flags are set for the cursor.
LASTNavigates to the last row in the cursor. If there are no rows in the cursor, then both the BOF and EOF flags are set for the cursor.
ABSOLUTENavigates to the Nth row specified. If the Nth row is greater than the number of rows in the cursor, then the EOF flag is set for the cursor. If there are no rows in the cursor, then both the BOF and EOF flags are set for the cursor.
RELATIVENavigates to the Nth row specified relative to the current row position. If the specified relative row is negative and the current row position minus the Nth row is less than 0, then the BOF flag is set for the cursor. If the current row position plus the Nth row is greater than the number of rows in the cursor, then the EOF flag is set for the cursor. If there are no rows in the cursor, then both the BOF and EOF flags are set for the cursor.

Information If you do not specify a fetch orientation, then the default orientation is to fetch from the current row position in the cursor.

You can use the BOF and EOF functions to determine if the BOF flag or EOF flag has been set on a cursor.

Specify a list of columns to only read the column values from a specific set of columns. If a list of columns is not specified and the INTO keyword is specified, then it is assumed that all column values should be read.

Use the INTO keyword to list one or more variables, parameters, or trigger NEWROW values into which the column values should be read.

Examples
-- The following job backs up all tables in all databases
-- defined in the current system at 11:00 PM every evening.

CREATE JOB Backup
RUN AS "System"
FROM DATE '2006-01-01' TO DATE '2010-12-31'
DAILY
BETWEEN TIME '11:00 PM' AND TIME '11:30 PM'
CATEGORY 'Backup'
BEGIN
   DECLARE DBCursor CURSOR FOR DBStmt;
   DECLARE DBName VARCHAR DEFAULT '';

   PREPARE DBStmt FROM 'SELECT * FROM Databases';

   OPEN DBCursor;

   FETCH FIRST FROM DBCursor ('Name') INTO DBName;

   WHILE NOT EOF(DBCursor) DO
      IF (DBName <> 'Configuration') THEN
         EXECUTE IMMEDIATE 'BACKUP DATABASE "' + DBName + '" AS "' +
                           CAST(CURRENT_DATE AS VARCHAR(10)) +
                           '-' + DBName + '" TO STORE "Backups" INCLUDE CATALOG';
      END IF;
      FETCH NEXT FROM DBCursor ('Name') INTO DBName;
   END WHILE;

   CLOSE DBCursor;
END

SQL 2003 Standard Deviations
This statement deviates from the SQL 2003 standard in the following ways:

DeviationDetails
Columns listThe list of columns to fetch from is an ElevateDB extension.
ExceptionsThe SQL standard dictates that exceptions are raised whenever a fetch operation cannot be completed due to a BOF or EOF condition or a row not being found. ElevateDB does not raise an exception in any of these cases and instead uses the BOF and EOF functions to indicate these conditions.
Image