FETCH (ABAP Keyword)

FETCH is a keyword used in SAP ABAP programming.
This tutorial covers its introduction & syntax details.

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
value 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 28-02.1995:

TABLES
SBOOK.
DATA C TYPE CURSOR,
WA LIKE SBOOK.
OPEN CURSOR C FOR SELECT *
FROM SBOOK
WHERE
CARRID = ‘LH ‘ AND
CONNID = ‘0400’ AND
FLDATE =
‘19950228’
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.