2

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

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
B0BBY
  • 1,012
  • 6
  • 24

1 Answers1

6

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

József Szikszai
  • 4,791
  • 3
  • 14
  • 24
  • 2
    Note that this will still copy "field1" to "field1" and "field3" to "field3" if fields with those names exist in both tables (in addition to the MAPPING instructions). If you want to avoid that, you need to add the [EXCEPT](https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-us/abencorresponding_constr_mapping.htm#!ABAP_ADDITION_2@2@) addition for any same-named fields you don't want to overwrite. – Philipp Dec 10 '21 at 12:12