ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Internal Tables → Processing Statements for Internal Tables → LOOP AT itab → LOOP AT itab - Basic Form
AT - Control Level Processing
Other versions: 7.31 | 7.40 | 7.54
Syntax
LOOP AT itab result
[cond].
[AT FIRST.
...
ENDAT.]
[AT NEW comp1.
...
ENDAT.
[AT NEW comp2.
...
ENDAT.
[...]]]
[ ... ]
[[[...]
AT END OF comp2.
...
ENDAT.]
AT END OF comp1.
...
ENDAT.]
[AT LAST.
...
ENDAT.]
ENDLOOP.
Extras
1. ... FIRST
2. ... {NEW}|{END OF} compi
3. ... LAST
Effect
The statement block of a LOOP
without the addition GROUP BY
can contain control structures for
control level processing.
The corresponding control statement is AT
. The statements AT
and ENDAT
define statement blocks that are executed at
control breaks, meaning in the transition of a
control level to another
control level. The additions of the statements AT
define the criteria for
a control break and hence the control levels. Control breaks between control levels are a result of
the row structure and the processing order in which the LOOP
reads the rows
of the internal table. Within the statement blocks, the statement SUM
can be specified to total the numeric components of a control level.
Apart from the special variants AT FIRST
and AT LAST
,
control levels are defined using the variant AT NEW
and the sequence of rows
in the internal table. These are the rows that have the same content in a control key defined in their
initial parts. It is possible, from a syntax point of view, to arrange AT
control structures in any order and to nest them, however effective control level processing is usually
only achieved by the arrangement above. Here, the control structures are specified in accordance with the arrangement of the components in the control key and are not nested.
The statement blocks within the AT
-ENDAT
control
structures are executed if an appropriate control break is made in the current table row. Statements
in the LOOP
-ENDLOOP
control structure that are
not executed within an AT
-ENDAT
control structure are executed in each pass of the loop. The following rules must be followed for effective control level processing:
-
A table key specified in
LOOP
in cond must be chosen so that it produces the required processing order of the imported rows. -
A restricting condition specified in
LOOP
incond
must select a contiguous row block in the internal table. -
The internal table cannot be modified within the
LOOP
loop. -
A work area
wa
specified in theLOOP
statement after the additionINTO
must be compatible with the row type of the table. -
A field symbol
<fs>
specified in the statementLOOP
after the additionASSIGNING
must be typed with the row type of the table. -
The content of a work area
wa
specified in the statementLOOP
after the additionINTO
cannot be modified.
If the addition INTO
is used in the statement LOOP
to assign the content of the current row to a work area wa
, its content is
modified as follows when the AT
-ENDAT
control structure is entered:
- The components of the current control key remain unchanged.
- All components with a character-like flat data type to the right of the current control key are set to character "*" in every position.
-
All the other components to the right of the current control key are set to their initial value.
When the AT
-ENDAT
control structure is exited, the content of the current table row is assigned to the entire work area wa
.
Notes
-
When the additions
ASSIGNING
andREFERENCE INTO
are used, the referenced table rows are not modified when entering and exiting theAT
-ENDAT
control structure. -
If
INTO
is used, note that " character-like" has a different definition in Unicode programs and obsolete non-Unicode programs, which means that identical structures can produce different results. -
Control level processing with the statement
AT
is not possible inLOOP
s across row groups with the addition GROUP BY, including member loops usingLOOP AT GROUP
. -
A common case is when the internal table is sorted exactly by the order of the components of its row
type, by the first component first, then the second component, and so on. In this case, control level
processing can also be expressed using a grouping with the addition
GROUP BY
. -
If possible, the use of the addition GROUP
BY is recommended, since the grouping is not determined by the structure of the rows and the processing order in this case.
Addition 1
... FIRST
Effect
The control level is defined by the first row of the internal table. The control break takes place when this row is read.
Note
In the control level AT FIRST
, the current control key does not contain any
components and all character-like components of the work area wa
are filled with "*" and all remaining components are set to their initial value.
Addition 2
... {NEW}|{END OF} compi
Effect
Control levels are defined by the beginning or end of a group of rows with the same content in the component
compi
(where i
= 1
,
2
, and so on) and in the components to the left of compi
.
The content of these components determines the control key. The control breaks take place when the content
of the component compi
or another component to the left of compi
changes.
The compi
components can be specified as described in
Specification of Components, with the limitation that access to data objects using references is not possible here. The following is therefore not possible:
- Specifying data objects using data references
-
Specifying attributes of objects using object references
Otherwise, the specified components can have any data type. The relevant comparison rules apply to the evaluation.
Notes
- In this case, control levels consist of the contiguous rows of the internal table that have the same content in an initial part.
-
An obsolete variant allows field symbols to also be specified, outside of classes
compi
.
Addition 3
... LAST
Effect
The control level is defined by the last row of the internal table. The control break takes place when this row is read.
Note
In the control level AT LAST
, the current control key does not contain any
components and all character-like components of the work area wa
are filled with "*" and all remaining components are set to their initial value.
Exceptions
Non-Catchable Exceptions
-
Cause: Invalid substring access when the control break criterion is specified dynamically.
Runtime Error:AT_BAD_PARTIAL_FIELD_ACCESS
-
Cause: The control break criterion is specified dynamically using the field symbol and the field symbol does not point to the
LOOP
output area.
Runtime Error:AT_ITAB_FIELD_INVALID
-
Cause: The control break criterion is specified dynamically using
(name) and the
name
field does not contain a valid substring name.
Runtime Error:ITAB_ILLEGAL_COMPONENT
-
Cause: Overflow of totals with
SUM
.
Runtime Error:SUM_OVERFLOW