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 using the INTOaddition, 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 SUMstatement with field symbols, since the statement is always applied to work areas.
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.
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 MODIFYstatement, using field symbols is worthwhile from a line width of 100 bytes onwards.
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>-col2 = 100.
READ TABLE itab WITH TABLE KEY col1 = 3 ASSIGNING
DELETE itab INDEX 3.
IF <fs> IS ASSIGNED.
WRITE '<fs> is assigned!'.
LOOP AT itab ASSIGNING <fs>.
WRITE: / <fs>-col1, <fs>-col2.
The list output is:
The example fills a sorted table ITAB with four 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, <fs> points to the third line of the table.