FETCH NEXT CURSOR dbcur {INTO|APPENDING} target.
This statement extracts the requested rows (by use of the addition INTO or APPENDING) from the resulting set of the database cursor (which is linked to the cursor variable dbcur) from the current cursor position and assigns these rows to the data objects specified in the resulting set.
The cursor variable dbcur must be a variable declared by the special predefined data type cursor, which was opened with the statement OPEN CURSOR, or to which an opened cursor was assigned. Otherwise, a treatable exception occurs.
The syntax and meaning of the addition INTO or APPENDING target are completely synonymous with the additions of the SELECT statement. If you specify non-table-type data objects after INTO, then one line is extracted. If an internal table is specified after INTO or APPENDING, then either all lines get extracted, or as many as specified in the addition PACKAGE SIZE.
The statement FETCH moves the position of the database
cursor (which is linked to dbcur) by the amount of extracted
lines to the next line to be extracted. If you extracted the last line of the resulting set in a
FETCH statement, then every subsequent FETCH
statement, in which dbcur is linked to the same database
cursor, sets sy-subrc to 4 without influencing the data
objects specified after INTO or APPENDING.
System Fields
The statement FETCH
sets the values of the system fields sy-subrc and sy-dbcnt.
sy-subrc | Description |
0 | At least one line was extracted from the resulting set. |
4 | No line was extracted. |
The statement FETCH sets sy-dbcnt
after every line extraction to the amount of the lines that have been extracted so far from the relevant
resulting set. If no line can be extracted, then sy-dbcnt is set to 0.
Consecutive FETCH statements that access the same resulting
set can have the different additions INTO or
APPENDING: The specification of work areas can be combined with the specification of internal
tables. In doing so, the addition CORRESPONDING FIELDS
is either not listed at all in any of FETCH statements,
or has to be listed in every statement. Moreover, the data types of all involved work areas
wa or the line types of the internal tables itab
must be identical. The specification of a bracketed list of data objects after
INTO can not be combined with the specification of work areas or internal tables, but every involved FETCH statement must contain such a list.
Reading of data of the database table SPFLI in packets of varying size with the help of two parallel cursors. The packet size is determined by the first cursor using the aggregation functioncount( * ) and using the second cursor for access. The variable control of the addition PACKAGE SIZE is not possible within a single SELECT statement.
DATA: BEGIN OF count_line,
carrid TYPE spfli-carrid,
count TYPE i,
END OF count_line,
spfli_tab TYPE TABLE OF spfli.
DATA: dbcur1 TYPE cursor,
dbcur2 TYPE cursor.
OPEN CURSOR dbcur1 FOR
SELECT carrid count(*) AS count
FROM spfli
GROUP BY carrid
ORDER BY carrid.
OPEN CURSOR dbcur2 FOR
SELECT *
FROM spfli
ORDER BY carrid.
DO.
FETCH NEXT CURSOR dbcur1 INTO count_line.
IF sy-subrc <> 0.
EXIT.
ENDIF.
FETCH NEXT CURSOR dbcur2
INTO TABLE spfli_tab PACKAGE SIZE count_line-count.
ENDDO.
CLOSE CURSOR: dbcur1,
dbcur2.