SAP FETCH

Get Example source ABAP code based on a different SAP table
  


Standard SAP Help for FETCH

FETCH

Short Reference
• FETCH ABAP Statement
• INTO FETCH
• APPENDING FETCH
• NEXT CURSOR FETCH


ABAP Syntax FETCH NEXT CURSOR dbcur {INTO|APPENDING}
target.

What does it do? This statement extracts the requested rows (using the addition INTO or APPENDING) from the results set of the database cursor (associated with the cursor variable dbcur) from the current cursor position and assigns these rows to the data objects specified in the results 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 handleable exception occurs.

The syntax and meaning of the addition INTO or APPENDING target are completely synonymous with the identical additions of the SELECT statement, with the exception that no LOB handle s can currently be created.
, wenn der Zusatz WITH HOLD beim Öffnen des Cursors verwendet
wurde.
INTHINT LOB Handles will be supported in a later Release but not for
INTHINT WITH HOLD

If non-table-like data objects are specified after INTO, then one row is extracted. If an internal table is specified after INTO or APPENDING, then either all rows are extracted, or as many as specified in the addition PACKAGE SIZE.

The statement FETCH moves the position of the database cursor (which is associated with dbcur) by the amount of extracted rows to the next row to be extracted. If the last row of the results set was extracted in a FETCH statement, each subsequent FETCH statement in which
dbcur is associated with the same database cursor sets sy-subrc to 4, without affecting 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-subrcMeaning 0At least one row was extracted from the results set. 4No row was extracted.

After every row extraction, the statement FETCH sets sy-dbcnt to the amount of rows extracted so far from the relevant results set. If an overflow occurs because the number or rows is greater than 2,147,483,647, sy-dbcnt is set to -1. If no row can be extracted, then sy-dbcnt is set to 0.

Latest notes:Consecutive FETCH statements that access the same results set can have the different additions INTO or APPENDING: Work areas can be specified together with internal tables. In doing so, the addition CORRESPONDING FIELDS is either not listed at all in any of the FETCH statements involved, or has to be listed in every statement. Moreover, the data types of all work areas wa involved or the row types of the internal tables itab must be identical. A bracketed list of data objects after INTO cannot be specified together with work areas or internal tables, but every involved FETCH statement must contain a list of this type.
It depends on the database system whether the database cursor in the database is closed implicitly after the extraction of the final row of the results set or not. For this reason, it is always better to use the CLOSE CURSOR statement explicitly.
Wenn in der INTO-Klausel die Erzeugung von
Leseströmen angegeben ist, schließt
FETCH alle mit der Ergebnismenge verknüpften Leseströme bevor
sie neue erzeugt.
INTHINT Implicit closing of reader streams not in current release



Example ABAP Coding
Reading of data from the database table
SPFLI in packets of varying size using two parallel cursors. The packet size is determined by the first cursor using the aggregation function count( * ) and using the second cursor for access. 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.

Return to menu