MOVE CORRESPONDING for Internal Tables in ABAP 7.4

0
171782

Dear SAPLearners, yes you heard me correct MOVE CORRESPONDING works for internal tables with ABAP 7.4 and in this blog post you will learn more about.

MOVE CORRESPONDING

We all know about this keyword and we have been using it since we learned it. For the benefit of me, I would like to define it once again.

MOVE-CORRESPONDING is used to move values from one structure to another.

Now that you can not wait to know how this works for internal tables. I have to keep you waiting a little longer and introduce you to a new ABAP 7.4 keyword, a constructor operator called CORRESPONDING

CORRESPONDING in ABAP 7.4

This CORRESPONDING constructor operator can be used to move data between two internal tables with a different set of columns. Let’s see this with an example.

Before that, if we had to do this in previous ABAP versions <7.4 we all know how painful it is to LOOP the first internal table, move the fields with some extra helper variables and finally APPEND to another internal table.

TYPES : BEGIN OF lty_demo1,
          col1 TYPE c,
          col2 TYPE c,
        END OF lty_demo1,

        BEGIN OF lty_demo2,
          col1 TYPE c,
          col3 TYPE c,
          col4 TYPE c,
        END OF lty_demo2.

DATA: itab1 TYPE STANDARD TABLE OF lty_demo1,
      itab2 TYPE STANDARD TABLE OF lty_demo2.

itab1 = VALUE #( ( col1 = 'A' col2 = 'B' )
                 ( col1 = 'P' col2 = 'Q' )
                 ( col1 = 'N' col2 = 'P' )
               ).

LOOP AT itab1 INTO ls_tab1.
  MOVE-CORRESPONDING ls_tab1 TO ls_tab2.
  ls_tab2-col3 = ls_tab1-col2.
  APPEND ls_tab2 TO itab2.

  CLEAR: ls_tab1,ls_tab2.
ENDLOOP.

But you are lucky, SAP got your back and understood your pain. So with ABAP 7.4 version  CORRESPONDING keyword easily moves data between internal tables.

Example #1 Basic Example

Lets start to learn more about with a simple example like below.

TYPES : BEGIN OF lty_demo1,
          col1 TYPE c,
          col2 TYPE c,
        END OF lty_demo1,

        BEGIN OF lty_demo2,
          col1 TYPE c,
          col3 TYPE c,
          col4 TYPE c,
        END OF lty_demo2.

DATA: itab1 TYPE STANDARD TABLE OF lty_demo1,
      itab2 TYPE STANDARD TABLE OF lty_demo2.

itab1 = VALUE #( ( col1 = 'A' col2 = 'B' )
                 ( col1 = 'P' col2 = 'Q' )
                 ( col1 = 'N' col2 = 'P' )
               ).
itab2 = CORRESPONDING #( itab1 ).

cl_demo_output=>write_data( itab1 ).
cl_demo_output=>write_data( itab2 ).
cl_demo_output=>display( ).

yes, that is it one line of ABAP code and the data is moved from one internal table to another internal table comparing the column names, if the column names does not match those values will be initialized – check the output below

MOVE CORRESPONDING for Internal Tables in ABAP 7.4

Also Read: What are inline declarations in ABAP 7.4

Example#2: CORRESPONDING With EXCEPT

Let’s make this example more complex, I have a column named COL2 in both internal tables but while copying I don’t want the value of this COL2 to be copied to another internal table.

TYPES : BEGIN OF lty_demo1,
          col1 TYPE c,
          col2 TYPE c,
        END OF lty_demo1,

        BEGIN OF lty_demo2,
          col1 TYPE c,
          col2 TYPE c,
          col3 TYPE c,
        END OF lty_demo2.

DATA: itab1 TYPE STANDARD TABLE OF lty_demo1,
      itab2 TYPE STANDARD TABLE OF lty_demo2.

itab1 = VALUE #( ( col1 = 'A' col2 = 'B' )
                 ( col1 = 'P' col2 = 'Q' )
                 ( col1 = 'N' col2 = 'P' )
               ).
itab2 = CORRESPONDING #( itab1 EXCEPT COL2 ).

cl_demo_output=>write_data( itab1 ).
cl_demo_output=>write_data( itab2 ).
cl_demo_output=>display( ).

and this is easier than you think with adding a keyword EXCEPT with the column name as an exception – check the output below COL2 data was not copied even though the same column exists in both internal tables.

CORRESPONDING Internal Tables ABAP 74 img2

Example #3: CORRESPONDING With MAPPING

Lets make this example even more complex, i have two different set of columns for the two internal tables and copy the column named COL2 of first internal table to COL3 of second internal table and the code is

TYPES : BEGIN OF lty_demo1,
          col1 TYPE c,
          col2 TYPE c,
        END OF lty_demo1,

        BEGIN OF lty_demo2,
          col1 TYPE c,
          col2 TYPE c,
          col3 TYPE c,
        END OF lty_demo2.

DATA: itab1 TYPE STANDARD TABLE OF lty_demo1,
      itab2 TYPE STANDARD TABLE OF lty_demo2.

itab1 = VALUE #( ( col1 = 'A' col2 = 'B' )
                 ( col1 = 'P' col2 = 'Q' )
                 ( col1 = 'N' col2 = 'P' )
               ).
itab2 = CORRESPONDING #( itab1 MAPPING COL3 = COL2 EXCEPT COL2 ).

cl_demo_output=>write_data( itab1 ).
cl_demo_output=>write_data( itab2 ).
cl_demo_output=>display( ).

yes just a lot more simple code by adding a MAPPING keyword for the two different columns of two different internal tables – check the output below

CORRESPONDING Internal Tables ABAP 74 img3

Conclusion

Congrats.. ! you have learned how MOVE-CORRESPONDING works for the internal table in ABAP 7.4 version. Stay tuned for more ABAP 7.4 tutorials. If you liked it ❤️, please share it! Thanks! 🙏

Continue Learning

Please feel free to comment and let us know your feedback. Subscribe for more updates.