SAP AT ITAB ABAP Statements



Get Example source ABAP code based on a different SAP table
  


ID AT-ITAB
• AT - itab ABAP Statement

AT, Group Level Processing
Short Reference

ABAP_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.

ABAP Addition
1 ... FIRST
2 ... ${NEW$}$|${END OF$} compi
3 ... LAST

What does it do?
The statement block of a LOOP without the addition GROUP BY can contain control structures for group level processing. The corresponding control statement is AT. The statements AT and ENDAT define statement blocks that are executed at group breaks, that is, in the transition of one group level to another group level. The additions of the statements AT define the criteria for a group break and hence the group levels. Group breaks between group levels are a result of the line structure and the processing order in which the LOOP reads the lines of the internal table.
BEGIN_SECTION VERSION 5 OUT Within the statement blocks, the statement SUM can be specified to total the numeric components of a group level.
END_SECTION VERSION 5 OUT
Apart from the special variants AT FIRST and AT LAST, group levels are defined using the variants AT NEW AT END OF and by the consecutive lines of the internal table specified in accordance with the processing order of the loop. These lines have the same content in a group key defined by their initial part. Here, all lines of the internal table are respected without evaluating any restricting conditions cond.
Although it is syntactically possible to arrange AT control structures in any order and to nest them, however, effective group level processing is usually only achieved by the arrangement above. Here, the control structures are listed in accordance with the arrangement of the components in the group key and are not nested.
The statement blocks within the AT-ENDAT control structures are executed if a corresponding group break takes place in the current table line. 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. If a table line with a group break is not read due to a restricting condition cond, the corresponding statement block is not executed.
The following rules must be observed for effective group level processing:
A table key specified in LOOP in cond must be chosen so that it produces the required processing order of the imported lines.
ABAP Addition STEP cannot be specified. Also other restricting conditions cond should not be specified. If a condition is specified, it should select consecutive line groups, including the first line in the group where the group break takes place.
The internal table must not be modified within the LOOP loop.
A work area wa specified in the LOOP statement after the addition INTO must be compatible with the line type of the table.
A field symbol <(><)> specified in the statement LOOP after the addition ASSIGNING must be typed with the line type of the table.
The content of a work area wa specified in the statement LOOP after the addition INTO cannot be modified.
If the addition INTO wa is used in the statement LOOP, the content of wa is overwritten when the AT-ENDAT control structure is entered, depending on the respective group level, and is reset to the content of the current line when the control structure is exited. When the additions ASSIGNING and REFERENCE INTO are used, the referenced table lines are not modified when entering and exiting the AT-ENDAT control structure.



Latest notes:

The most common case is when the internal table is sorted exactly by the order of the components of its line type, that is, by the first component first, then by the second component, and so on. In this case, group 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 does not depend on the structure of the lines and the processing order in this case.
Group level processing with the statement AT is not possible in LOOP statements across line groups with the addition GROUP BY, including member loops using LOOP AT GROUP.
The group level processing described here can produce unexpected results when a restricting condition cond is used, which means that extended syntax check messages can occur.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_ABEXA
Group Level Processing
ABAP_EXAMPLE_END
• FIRST AT - itab

ABAP Addition

What does it do?
The group level is defined by the first line of the internal table. The group break takes place when this line is read. In the group level AT FIRST, the current group key does not contain any components.
If the addition INTO wa is used in the statement LOOP, all character-like components of the work area wa are filled with * when the control structure AT FIRST is entered and all other components are set to their initial value. In the case of an elementary line type, this applies to the entire work area. When the AT- ENDAT control structure is exited, the content of the current table line is reassigned to the work area wa.



Latest notes:

If the first line of the internal table is not read due to a restricting condition cond, the statement block of the control structure AT FIRST is not executed.
NON_V5_HINTS
ABAP_HINT_END



Example ABAP Coding

Display of a header and the first line of an internal table at the group level AT FIRST.
ABEXA 00041
ABAP_EXAMPLE_END
• NEW AT - itab
• END OF AT -itab

ABAP Addition

What does it do?
Group levels are defined by the beginning or end of a group of lines with the same content in the component compi (where i = 1, 2, and so on) and in the components on the left of compi. The content of these components determines the group key. The group breaks take place when the content of the component compi or another component on the left of compi changes.
The compi components can be specified as described in Specification of Components, with the restriction 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 corresponding comparison rules apply to the evaluation. In the case of an elementary line type, only table_line can be specified as the group key.
If the addition INTO wa is used in the statement LOOP, the content of wa is modified as follows when the AT-ENDAT control structure is entered:
The components of the current group key remain unchanged.
All components with a character-like flat data type on the right of the current control key are set to the character * in every place.
All the other components to the right of the current group key are set to their initial value.
In the case of an elementary line type, the entire work area corresponds to the current group key and remains unchanged. When the AT- ENDAT control structure is exited, the content of the current table line is reassigned to the work area wa.



Latest notes:

Group levels defined in this way consist of the consecutive lines of the internal table that have the same content in an initial part.
If the first or last line of a line group is not read due to a restricting condition cond, the statement block of the control structure AT NEW or AT END OF is not executed.
NON_V5_HINTS
An obsolete variant allows field symbols to also be specified, outside of classes compi.
ABAP_HINT_END



Example ABAP Coding

Display of the carrid column as a header for the chained content of the associated flight numbers.
ABEXA 00042
ABAP_EXAMPLE_END



Example ABAP Coding

The first line of the first line group is not read due to the addition FROM 2, which means the associated statement block after AT NEW table_line is not executed. The program therefore produces a message from the extended program check.
ABEXA 00043
ABAP_EXAMPLE_END
• LAST AT - itab

ABAP Addition

What does it do?
The group level is defined by the last line of the internal table. The group break takes place when this line is read. In the group level AT LAST, the current group key does not contain any components.
If the addition INTO wa is used in the statement LOOP, all character-like components of the work area wa are filled with * when the control structure AT LAST is entered and all other components are set to their initial value. In the case of an elementary line type, this applies to the entire work area. When the AT- ENDAT control structure is exited, the content of the current table line is reassigned to the work area wa.



Latest notes:

If the last line of the internal table is not read due to a restricting condition cond , the statement block of the control structure AT LAST is not executed.
NON_V5_HINTS
ABAP_HINT_END



Example ABAP Coding

Display of cumulated content during the processing of the last line.
ABEXA 00044
ABAP_EXAMPLE_END



Runtime Exceptions


Non-catchable Exceptions
Reason for error:
Invalid substring access when the group break criterion is specified dynamically.
Runtime error:
AT_BAD_PARTIAL_FIELD_ACCESS
Reason for error:
The group break criterion is specified dynamically using the field symbol and the field symbol does not point to the LOOP output range.
Runtime error:
AT_ITAB_FIELD_INVALID
Reason for error:
The group break criterion is specified dynamically using (name) and the name field does not contain a valid substring name.
Runtime error:
ITAB_ILLEGAL_COMPONENT
Reason for error:
Overflow of totals with SUM.
Runtime error:
SUM_OVERFLOW
ABAP_NONCAT_END

Return to menu