SAP LOOP AT DBTAB ABAP Statements

Get Example source ABAP code based on a different SAP table
  


Standard SAP Help for LOOP_AT_DBTAB

LOOP AT dbtab

Short Reference
• LOOP AT dbtab ABAP Statement

ABAP Syntax(Obsolete)LOOP AT { dbtab | *dbtab } [VERSION vers].
...
ENDLOOP.

ABAP Addition
... VERSION vers

What does it do? The statements LOOP and ENDLOOP define a loop around a statement block (not allowed in classes). For dbtab , you must specify a database table that begins with 'T' and has a maximum length of five characters. For the database table dbtab, a table work area or an additional table work area must be declared using the statement TABLES. All components of the table work area that match the primary key fields of the database table dbtab must be character-type.

In each loop run, the statement LOOP reads a line from the database table dbtab and assigns its content either to the table work area, or on specification of *dbtab, to the additional table work area. The lines to be read are determined by the content of the components of the table work area, which correspond with the primary key fields of the database table dbtab. Before the first loop run, the content of these components is taken as the search key, reading from the left, and the database tab le is searched generically for suitable entries. The search key handles blanks as if they match all values.

If a database table is specified that does not begin with 'T', then the first letter is implicitly replaced by 'T'. The loop is not executed if the database table does not exist

System Fields sy-subrcMeaning 12The database table was not found.
INTHINT sy-subrc is set to 16, if access to the database specified
INTHINT by VERSION is not allowed by the dynamic package check.

Latest notes:This form of the LOOP-loop is not allowed in classes. It must be replaced by the statement SELECT .
ABAP Code Snippet The obsolete access statements do not support automatic client handling. The client identifier of a database table must be specified explicitly. The application programs are only to work with data for the current client. In systems with multitenancy, this is checked by the ABAP runtime environment.
ABAP Code Snippet
• VERSION LOOP AT dbtab (obsolete)

ABAP Addition

What does it do? If the addition VERSION is specified, then the database table dbtab is not read, and the table whose name is composed of 'T' and the content of vers is read instead. vers expects a data object with a maximum of four characters, of type c. The content of the row is still assigned to the table work area of dbtab or *dbtab and its type is cast. If the table work area is too short, a runtime error occurs.



Example ABAP Coding
Sequential reading of rows from database table
T100. TABLES t100.

t100 = space.
t100-sprsl = 'E'.
t100-arbgb = 'BC'.
t100-msgnr = '1'.

LOOP AT t100.
...
ENDLOOP.

The Open SQL syntax to be used instead reads:
NEXT EXAMPLE DATA wa TYPE t100.

SELECT *
FROM t100
INTO wa
WHERE sprsl = 'E' AND
arbgb = 'BC' AND
msgnr LIKE '1%'.
...
ENDSELECT.

Return to menu