MOVE-CORRESPONDING for Internal Tables in ABAP 7.4

0
522

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 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 constructor operator can be used to move data between two internal tables with a different set of columns. Lets see this with an example.

Before that lets say if we were have 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

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

Example #2

Lets make this example more complex, i have a column named COL2 in both internal 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 more easy 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.

Example #3

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 belowCongrats.. ! you have learned how CORRESPONDING works for internal table in ABAP 7.4 version. Stay tuned for more ABAP 7.4 tutorials.

Also Read: What are inline declarations in ABAP 7.4

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

If you liked it, please share it! Thanks!