Replace value of an internal table field

images/thumbnail.jpg - Thumbnail

Now that ABAP has comprehensions, there are a number of superpowers that aren’t always obvious. Here, I’ll show you how to change the value of a single field in all, several or just one record of an internal table.

Imagine you have an internal table:

SELECT * FROM t001 INTO TABLE @DATA(companies).

Previously, you would have done this imperatively:

LOOP AT companies INTO FIELD-SYMBOL(<company>).
  <company>-butxt = 'Abapinho'.
ENDLOOP.

Now, using functional syntax, you do it like this:

FINAL(hacked_companies) = VALUE t_t001(
  FOR company IN companies
  ( VALUE #( BASE company
             butxt = 'Abapinho' ) ) ).

If you only want to change the field in some records, or even just one, add a WHERE clause:

FINAL(hacked_companies) = VALUE t_t001(
  FOR company IN companies
  WHERE ( bukrs = 'SAP' )
  ( VALUE #( BASE company
             butxt = 'Abapinho' ) ) ).

The trick is to use BASE together with company to define the starting point and then change only the fields you want. In this case, I’ve only changed the BUTXT.

Greetings from Abapinho.