Changing Lines 

To change a single line of any internal table, use the MODIFY statement. You can either use the table key to find and change a single line using its key, or find and change a set of lines that meet a certain condition. If the table has a non-unique key and there are duplicate entries, the first entry is changed.

Changing a Line Using the Table Key

To change a single line, use the following statement:

MODIFY TABLE <itab> FROM <wa> [TRANSPORTING <f1> <f 2> ...].

The work area <wa>, which must be compatible with the line type of the internal table, plays a double role in this statement. Not only it is used to find the line that you want to change, but it also contains the new contents. The system searches the internal table for the line whose table key corresponds to the key fields in <wa>.

The system searches for the relevant lines as follows:

Linear search, where the runtime is in linear relation to the number of table entries. The first entry found is changed.

Binary search, where the runtime is in logarithmic relation to the number of table entries. The first entry found is changed.

The entry is found using the hash algorithm of the internal table. The runtime is independent of the number of table entries.

If a line is found, the contents of the non-key fields of the work area are copied into the corresponding fields of the line, and SY-SUBRC is set to 0. Otherwise, SY-SUBRC is set to 4. If the table has a non-unique key and the system finds duplicate entries, it changes the first entry.

You can specify the non-key fields that you want to assign to the table line in the TRANSPORTING addition. You can also specify a field <f i > dynamically as the contents of a field <n i > in the form (<n i >). If <n i > is empty when the statement is executed, it is ignored. You can restrict the search to partial fields by specifying offset and length.

For tables with a complex line structure, the usage of the transporting option results in better performance, if the system must not transport unnecessary table-like components.

Changing Several Lines Using a Condition

To change one or more lines using a condition, use the following statement:

MODIFY <itab> FROM <wa> TRANSPORTING <f1> <f 2> ... WHERE <cond>.

This processes all of the lines that meet the logical condition <cond>. The logical condition can consist of more than one comparison. In each comparison, the first operand must be a component of the line structure. If the table lines are not structured, the first operand can also be the expression TABLE LINE. The comparison then applies to the entire line.

The work area <wa>, which must be compatible with the line type of the internal table, contains the new contents, which will be assigned to the relevant table line using the TRANSPORTING addition. Unlike the above MODIFY statement, the TRANSPORTING addition is not optional here. Furthermore, you can only modify the key fields of the internal table if it is a standard table. If at least one line is changed, the system sets SY-SUBRC to 0, otherwise to 4.

Examples

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.
  LINE-COL1 = SY-INDEX.
  LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.

LINE-COL1 = 2. LINE-COL2 = 100.

MODIFY TABLE ITAB FROM LINE.

LOOP AT ITAB INTO LINE.
  WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         1        1
         2      100
         3        9
         4       16

The program fills a hashed table with a list of square numbers. The MODIFY statement changes the line of the table in which the key field COL1 has the value 2.

DATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.

DATA ITAB LIKE HASHED TABLE OF LINE WITH UNIQUE KEY COL1.

DO 4 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
INSERT LINE INTO TABLE ITAB.
ENDDO.

LINE-COL2 = 100.

MODIFY ITAB FROM LINE TRANSPORTING COL2
WHERE ( COL2 > 1 ) AND ( COL1 < 4 ).

LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2.
ENDLOOP.

The output is:

         1        1
         2      100
         3      100
         4       16

The program fills a hashed table with a list of square numbers. The MODIFY statement changes the lines of the table where the content of field COL2 is greater than 1 and the content of field COL1 is less than 4.