Search code examples
abapinternal-tables

MOVE-CORRESPONDING for internal tables?


I want MOVE fields from itab1 to itab2 based on their field names. I have tried following:

CLEAR itab2.
MOVE-CORRESPONDING itab1 TO itab2.

This is working out, but only as long as the FIELDS are named equal.

Now I want to to something like that:

CLEAR itab2.
MOVE-CORRESPONDING itab1-field1 TO itab2-field2.
MOVE-CORRESPONDING itab1-field3 TO itab2-field4.

and so on.. But each time I try to do that I get following error "itab1-field1" is not a structure or an internal table.

I have also tried to write it like this MOVE-CORRESPONDING <itab1>-field1 but this doesn't work either.

How can I achieve what I want? Thanks for trying helping me out..


Solution

  • From ABAP 7.4 it can be done with the CORRESPONDING ... MAPPING statement:

    itab_target = CORRESPONDING #( itab_source
                                   MAPPING field2 = field1
                                           field4 = field3 ).
    

    Target will be based on source, where the fields have the same name, otherwise the MAPPING will be used (target-field2 will be source-field1, etc.) This works with structures and internal tables as well.

    ABAP Help CORRESPONDING