Start of Content Area

Changing Lines  Locate the document in its SAP Library structure

To change a single line of any internal table, use the MODIFYstatement. 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 f2 ...].

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 table types as follows:

·        Standard tables

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

·        Sorted tables

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

·        Hashed tables

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 the fields f1 f2 … dynamically using (n1) (n2) … as the contents of a field n1 n2 …. If <ni> is empty when the statement is executed, it is ignored. You can also restrict all fields f1 f2 … to subfields by specifying offset and length.

For tables with a complex line structure, the use of the TRANSPORTINGaddition results in better performance, provided the system does not have to 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 f2 ... WHERE cond.

This processes all of the lines that meet the logical condition cond. The logical expression cond 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. If the line type of the internal table contains object reference variables as component comp or if the entire line type is a reference variable, the attributes of the attr object to which the respective line reference points can be specified as comparison values using comp->attr or table_line->attr.

The work area wa, which must be compatible with the line type of the internal table, contains the new contents, which in turn will be assigned to the relevant table line using the TRANSPORTING addition. Unlike the above MODIFYstatement, 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.

Example

REPORT demo_int_tables_modify .

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

Example

REPORT demo_int_tables_modify_transp.

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

Changing Table Lines Using the Index

You can use the MODIFY statement to change lines in tables using their index. There is also an obsolete variant of the WRITE TOstatement that you can use to modify lines in standard tables.

Changing Single Lines using MODIFY

To change a line using its index, use the following statement:

MODIFY itab FROM wa [INDEX idx] [TRANSPORTING f1 f2... ].

The work area wa specified in the FROM addition replaces the addressed line in the itab table. The work area must be convertible into the line type of the internal table.

If you use the INDEX option, the contents of the work area overwrite the contents of the line with the index idx. If the operation is successful, sy-subrc is set to 0. If the internal table contains fewer lines than idx, no line is changed and sy-subrc is set to 4.

Without the INDEX addition, you can only use the above statement within a LOOP. In this case, you delete the current line. idx is implicitly set to sy-tabix .

When you change lines in sorted tables, remember that you must not change the contents of key fields, and that a runtime error will occur if you try to replace the contents of a key field with another value. However, you can assign the same value.

The TRANSPORTING addition allows you to specify the fields that you want to change explicitly in a list.  If you change a sorted table, you may only specify non-key fields.

Example

REPORT demo_int_tables_modify_ind.

DATA: BEGIN OF line,
        col1 TYPE i,
        col2 TYPE i,
      END OF line.

DATA itab LIKE TABLE OF line.

DO 3 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.

ENDDO.

LOOP AT itab INTO line.
  IF sy-tabix = 2.
    line-col1 = sy-tabix * 10.
    line-col2 = ( sy-tabix * 10 ) ** 2.
    MODIFY itab FROM line.

  ENDIF.
ENDLOOP.

LOOP AT itab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.

This produces the following output:

         1         1          1
         2        20        400
         3         3          9

Here, a sorted table itab is created and filled with three lines. The second line is replaced by the contents of the work area line.

Example

REPORT demo_int_tables_modify_ind_ind.

DATA name(4) TYPE c VALUE 'col2'.

DATA: BEGIN OF line,
         col1 TYPE i,
         col2 TYPE i,
      END OF line.

DATA itab LIKE SORTED TABLE OF line WITH UNIQUE KEY col1.

DO 4 TIMES.
  line-col1 = sy-index.
  line-col2 = sy-index ** 2.
  APPEND line TO itab.

ENDDO.

line-col2 = 222.
MODIFY itab FROM line INDEX 2 TRANSPORTING (name).

line-col1 = 3.
line-col2 = 333.
MODIFY itab FROM line INDEX 3.

LOOP AT itab INTO line.
  WRITE: / sy-tabix, line-col1, line-col2.
ENDLOOP.

The list output is:

         1         1          1
         2         2        222
         3         3        333
         4         4         16

The example fills a sorted table with four lines. In the second and third lines, the component col2 is modified. If the third line were to be changed so that the value of line-col1 was no longer 3, a runtime error would occur, since the key fields of sorted tables may not be changed.

 

 

End of Content Area