SAP FETCH ABAP Statements



Get Example source ABAP code based on a different SAP table
  


ID FETCH
• FETCH ABAP_STATEMENT_OSQL
• INTO FETCH
• APPENDING FETCH
• NEXT CURSOR FETCH

FETCH
Short Reference

ABAP_SYNTAX
FETCH NEXT CURSOR dbcur INTO$|APPENDING ....

What does it do?
The AB-SQL statement FETCH takes the rows requested by the INTO or APPENDING addition from the result set of the database cursor linked with the cursor variable dbcur from the current cursor position and assigns them to the data objects specified there.
The cursor variable dbcur must be a host variable declared with the special predefined data type cursor, which was opened with the statement OPEN CURSOR or that was assigned an open cursor. Otherwise, a catchable exception of the class CX_SY_OPEN_SQL_DB is raised.
The syntax and meaning of the addition INTO or APPENDING are the same as the identically named additions of the SELECT statement, with the exception that no inline declarations can be made there without the addition NEW and no LOB handles can be created.
BEGIN_SECTION SAP_INTERNAL_HINT
LOB Handles will be supported in a later release but not for WITH HOLD.
END_SECTION SAP_INTERNAL_HINT
If non-table-like data objects are specified after INTO, one row is extracted. If an internal table is specified after INTO or APPENDING, 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 that is linked with dbcur by the number of extracted rows to the next row to be extracted. If the last row of the result set was extracted in a FETCH statement, each subsequent FETCH statement in which dbcur is linked 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 result set. 4No row was extracted.
After every row extraction, the statement FETCH sets sy-dbcnt to the number of rows extracted so far from the relevant result 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, sy-dbcnt is set to 0.



Latest notes:

Consecutive FETCH statements that access the same result set can have the different additions INTO or APPENDING. The specification of work areas can be combined with any internal table specifications and various combinations of PACKAGE SIZE can be specified as well. In doing so, the addition CORRESPONDING FIELDS must either not be 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. The specifications of a parenthesized list of data objects after INTO cannot be combined with the specification of 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 result set or not. For this reason, it is always better to use the CLOSE CURSOR statement explicitly.
If a CDS view is defined as a replacement object for a DDIC database table or DDIC database view specified as a data source of the SELECT statement of OPEN CURSOR, the statement FETCH accesses the CDS view and not the DDIC database table or the DDIC database view.
The specifications of host variables without the escape character @ is obsolete. The escape character @ must be specified in the strict modes of the syntax check from ABAP_RELEASE ABAP_740_SP05 .
If a statement OPEN CURSOR is checked in accordance with the rules for ABAP_STRICT_760 strict mode / from ABAP_RELEASE ABAP_760 , this also applies to every statement FETCH that accesses the open database cursor. Conversely, a strict syntax check mode in the statement FETCH from ABAP_STRICT_777 ABAP_RELEASE ABAP_777 / triggered by the use of NEW in the INTO clause also applies the strict mode to the associated statement OPEN CURSOR. A violation of the rules specified by FETCH in OPEN CURSOR raises an exception of the class CX_SY_DYNAMIC_OSQL_SEMANTICS.
BEGIN_SECTION SAP_INTERNAL_HINT
Implicit closing of reader streams not in current release.
END_SECTION SAP_INTERNAL_HINT
ABAP_HINT_END



Example ABAP Coding

Reading of data from the DDIC 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.
ABEXA 00260
ABAP_EXAMPLE_END

Return to menu