Search code examples
abapinternal-tables

What is the shortest notation for updating a column in an internal table?


I have an ABAP internal table. Structured, with several columns (e.g. 25). Names and types are irrelevant. The table can get pretty large (e.g. 5,000 records).

| A   | B   | ... |
| --- | --- | --- |
| 7   | X   | ... |
| 2   | CCC | ... |
| 42  | DD  | ... |

Now I'd like to set one of the columns (e.g. B) to a specific constant value (e.g. 'Z').

What is the shortest, fastest, and most memory-efficient way to do this?

My best guess is a LOOP REFERENCE INTO. This is pretty efficient as it changes the table in-place, without wasting new memory. But it takes up three statements, which makes me wonder whether it's possible to get shorter:

LOOP AT lt_table REFERENCE INTO DATA(ls_row).
  ls_row->b = 'Z'.
ENDLOOP.

Then there is the VALUE operator which reduces this to one statement but is not very efficient because it creates new memory areas. It also gets longish for a large number of columns, because they have to be listed one by one:

lt_table = VALUE #( FOR ls_row in lt_table ( a = ls_row-a
                                             b = 'Z' ) ).

Are there better ways?


Solution

  • The following code sets PRICE = 0 of all lines at a time. Theoritically, it should be the fastest way to update all the lines of one column, because it's one statement. Note that it's impossible to omit the WHERE, so I use a simple trick to update all lines.

    DATA flights TYPE TABLE OF sflight.
    DATA flight TYPE sflight.
    
    SELECT * FROM sflight INTO TABLE flights.
    flight-price = 0.
    MODIFY flights FROM flight TRANSPORTING price WHERE price <> flight-price.
    

    Reference: MODIFY itab - itab_lines