Internal tables can be read by accessing individual rows (using
READ TABLE or
or sequentially (using
LOOP AT). In both cases, the following output behavior can be defined by using the statements with the following additions:
- The addition
INTOcopies the content of the row to an appropriate data object.
- The addition
ASSIGNINGassigns the read row to a field symbol, which enables the row to be addressed directly.
REFERENCE INTOaddition sets a data reference to the read row.
As well as for exports, the
additions can also be used for the
MODIFY statements, where they create references to the row being edited.
Choose appropriate output behavior
When reading rows of internal tables, select an appropriate output behavior. The rule of thumb is:
- Copy to a work area if the row type is narrow and the read row is not to be modified.
- Assign to a field symbol if the row type is wide or deep and the read row is to be modified.
- Set a data reference if the row type is wide or deep and a reference to the read row is to be passed.
The criteria for selecting the output behavior are the processing speed, on the one hand, and what is to be done with the read row, on the other hand:
- If the content of the read row is to be modified, the addition
ASSIGNINGor (in the case of table expressions) the appropriate result should usually be used. This allows direct access to the row using the value semantics and removes the need for a
MODIFYoperation later on.
- If a reference to the read row is required that can be processed using reference semantics, the addition
REFERENCE INTOor (in the case of table expressions) the appropriate result is to be used.
- If the content of the read row is not to be modified, any of these procedures can be used. The row
type of the table is significant for performance. If the table row is wide or contains deep components
(for example, strings or other tables), reads are usually faster if
REFERENCE INTOis used instead of
INTO. The way they are used is the determining factor for selecting which of the two should be used.
INTOis faster (at least for the
READstatement) than configuring the administration that is required for dynamic access. For the statement
LOOP, these costs are incurred only once, so that using
ASSIGNINGor REFERENCE INTO is always recommended above a certain number of rows. In contrast,
INTOshould always be used if the target area is to be modified without this affecting the internal table.
Besides the processing speed, it is also important that the source code can be understood. If the recommendations
mentioned are kept, reading a table with the addition
ASSIGNING (but also
REFERENCE INTO) indicates to the reader that the table content is potentially
changed. Reading a table with the
INTO addition, on the other hand, indicates that the table will not be modified.
For table expressions, the information here applies to the selection of the appropriate result.
The following source code shows the assignment of rows of an internal table to a work area with the
aim of modifying the read rows. For this modification, however, an additional statement,
MODIFY, is required, and two unnecessary copy processes take place for each loop pass.
LOOP AT itab INTO wa.
wa = ...
MODIFY itab FROM wa.
The following source code corrects the above example; here, a field symbol is used for direct access to modify the read rows. No unnecessary copy costs are incurred.
LOOP AT itab ASSIGNING <fs>.
<fs> = ...