FETCH

Basic form

FETCH NEXT CURSOR c target.

Effect

Uses the cursor c to read the next line or lines from the dataset of a database table determined by OPEN CURSOR. The cursor must be a variable of the type CURSOR and must be explicitly opened with OPEN CURSOR . To specify the target area into which you read the selected data, use INTO clause target.

FETCH belongs to the Open SQL command set.

After each execution of the FETCH statement, the system field SY-DBCNT contains the number of lines read so far.

The Return Code is set as follows:

SY-SUBRC = 0:
At least one line was read.
SY-SUBRC = 4:
No line was read.

Example

Output the passenger list for the Lufthansa flight 0400 on 02/28/2001:

DATA:   c  TYPE CURSOR,
        wa TYPE sbook.

OPEN CURSOR c FOR SELECT * FROM sbook
  WHERE
    carrid   = 'LH '      AND
    connid   = '0400'     AND
    fldate   = '20010228'
  ORDER BY PRIMARY KEY.

DO.
  FETCH NEXT CURSOR c INTO wa.
  IF SY-SUBRC <> 0.
    CLOSE CURSOR C. EXIT.
  ENDIF.
  WRITE: / wa-bookid, wa-customid,   wa-custtype,
           wa-smoker, wa-luggweight, wa-wunit,
           wa-invoice.
ENDDO.

Related

SELECT, INTO clause

Additional help

Reading Data Using a Cursor