Access Using Field Symbols
When you read table entries using READ or in a LOOP, you can assign them to a field symbol using the addition
... ASSIGNING <FS>
The field symbol <FS> points directly to the assigned line in memory. Unlike work areas, in which the contents of the line are only available indirectly, field symbols allow you to read and change table entries directly.
Remember when you access internal tables using field symbols that you must not change the contents of the key fields of sorted or hashed tables. If you try to assign a new value to a key field using a field symbol, a runtime error occurs. Note that you cannot use the SUM statement with field symbols, since the statement is always applied to work areas.
Advantages of Field Symbols
When you read from an internal table, there are no overheads for copying the table line to the work area. When you change an internal table with the MODIFY statement, you must first fill a work area with values, and then assign them to the internal table. If you work with field symbols instead, you do not have this overhead. This can improve performance if you have large or complex internal tables. It also makes it easier to process nested internal tables.
Overheads of READ
Note that internal overheads arise when you access internal tables using field symbols. After a READ statement with a field symbol, the system has to register the assignment. When you delete a table line to which a field symbol is pointing, the system also has to unassign the field symbol to prevent it from pointing to an undefined area.
When you read individual table lines, it is worth using field symbols with the READ statement for tables with a line width of 1000 bytes or more. If you also change the line using the MODIFY statement, using field symbols is worthwhile from a line width of 100 bytes onwards.
Overheads of LOOP
To minimize the overheads incurred by using field symbols in loop processing, the system does not register the assignment of each current line to the field symbol. Instead, it registers a general assignment between a line of the table and the field symbol. When the loop is finished, the line processed in the last loop pass is assigned to the field symbol.
Consequently, it is worth using field symbols in a LOOP when the internal table has as few as 10 lines. However, it is not possible to reassign the field symbol to another field or unassign it altogether within the loop. If you include the statements ASSIGN, UNASSIGN, or the ASSIGNING addition for the same field symbol within the loop block, a runtime error occurs.
DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.
FIELD-SYMBOLS <FS> LIKE LINE OF ITAB.
DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
APPEND LINE TO ITAB.
READ TABLE ITAB WITH TABLE KEY COL1 = 2 ASSIGNING <FS>.
<FS>-COL2 = 100.
READ TABLE ITAB WITH TABLE KEY COL1 = 3 ASSIGNING <FS>.
DELETE ITAB INDEX 3.
IF <FS> IS ASSIGNED.
WRITE '<FS> is assigned!'.
LOOP AT ITAB ASSIGNING <FS>.
WRITE: / <FS>-COL1, <FS>-COL2.
The output is:
The example fills a sorted table ITAB with 4 lines. The second line is assigned to the field symbol <FS> (which has the same type), and modified using it. The third line is assigned to <FS> and then deleted. Consequently, the logical expression in the IF statement is untrue. <FS> is used to display the table lines in the LOOP. Afterwards, it points to the third line of the table.