3.2. Manipulation - Corresponding

 Corresponding Operator

You must have guessed that this will be replacement of move-corresponding from ECC . You are absolutely correct but this operator is much more powerful then move-corresponding. We can even use internal table in addition to structures and the feature which I like the most is that we can provide our own mapping rule which means it is no limited to columns with name.

I addition to all the above feature we can even mention the field which we don’t want to get transferred. 
So lets start with the real example to support all the above claims 😊.
 

To show examples for this I am using similar structure and table which are created initially. Let recall that again : 


TYPES : BEGIN OF ty_int1,
          name TYPE char10,
          value TYPE char20,
          END OF ty_int1.
  TYPES : tt_int1 TYPE STANDARD TABLE OF ty_int1 WITH DEFAULT KEY.   " internal table


  TYPES : BEGIN OF ty_deep,
           region TYPE char5,
           country TYPE char10,
           ls_work_info TYPE tt_int1,
           END OF ty_deep,
           tt_deep TYPE STANDARD TABLE OF ty_deep WITH DEFAULT KEY.


  DATA(lt_deep) = VALUE tt_deep(
                               (
                                 region = 'APJ' country = 'INDIA'
                                 ls_work_info = VALUE #(
                                                   ( name = 'TECH' value = 'SAP' )
                                                   ( name = 'DOMAIN' value = 'ABAP' )
                                                                         )
                                )   " first row
                               (
                                 region = 'EMEA' country = 'UK'
                                 ls_work_info = VALUE #(
                                               ( name = 'TECH' value = 'ML' )
                                               ( name = 'DOMAIN' value = 'PYTHON' )
                                                                           )
                                ) " second row
                              ).


DATA(lt_deep2) = VALUE tt_deep(
                               (
                                 region = 'APJ2' country = 'INDIA2'
                                 ls_work_info = VALUE #(
                                       ( name = 'TECH2' value = 'SAP2' )
                                        ( name = 'DOMAIN2' value = 'ABAP2' )
                                                                            )
                                )   " first row
                               (
                                 region = 'EMEA2' country = 'UK2'
                                 ls_work_info = VALUE #(
                                            ( name = 'TECH2' value = 'ML2' )
                                              ( name = 'DOMAIN2' value = 'PYTHON2' )
                                                                              )
                                ) " second row
                              ).


  DATA(lt_int1) = VALUE tt_int1(                                                                           ( name = 'TECH' value = 'SAP' )
                                 ( name = 'DOMAIN' value = 'ABAP' )
                               ).

 
 
 

 
TYPES : BEGIN OF ty_int2,
              name TYPE char10,
              value TYPE char20,
              exp   TYPE i,
              END OF ty_int2,
              tt_int2 TYPE STANDARD TABLE OF ty_int2 WITH DEFAULT KEY.

3.2.1. Passing the data between two structures/internal table 
 
Create two new work area ls_source and fill data ,
DATA(ls_source) = VALUE ty_int1( name = 'TECH' value = 'SAP' ).
DATA(ls_target) = VALUE ty_int2( name = 'DOMAIN' value = 'ABAP' exp = '5' ). 
 
*OLD
MOVE-CORRESPONDING ls_source TO ls_target.   "Overwrite ls_target
 

*NEW
DATA(ls_target1) = CORRESPONDING ty_int1( ls_source ).
 
*It will change the column to initial if is not found in the structure as shown below
 
 

3.2.2. Using keyword 'BASE'

* Changing back to original
ls_target = VALUE #( name = 'DOMAIN' value = 'ABAP' exp = '5' ). 
 
* Use of BASE to keep the values of target table which are not present in source table
ls_target = CORRESPONDING #( BASE ( ls_target ) ls_source ). 
 
* Above will keep the column as it is if it is not found in the structure as shown below 
 

 
Same way we can even pass data in completely new table.
 
DATA(ls_target1) = CORRESPONDING ty_int2( BASE ( ls_target ) ls_source ).
 
               
 
3.2.3. Use of ‘Corresponding’ With internal table 

*Create new table with extra column added as 'EXP'

DATA(lt_target2) = VALUE tt_int2(   
                                                     ( name = 'TECH2' value = 'SAP2' exp = '5' )                                                                                        ( name = 'DOMAIN2' value = 'ABAP2' exp = '10')

                                     ).


 
lt_target2 = CORRESPONDING #( lt_int1 ).


* Changing back to original

 lt_target2 = VALUE #(                                  
                     ( name = 'TECH2' value = 'SAP2' exp = '5' )
                     ( name = 'DOMAIN2' value = 'ABAP2' exp = '10')
                    ).


lt_target2 = CORRESPONDING #( BASE ( lt_target2 ) lt_int1 ) .   “ Will append the values




3.2.4. Use of 'EXCEPT' :  When we don't want to map a particular field with corresponding
 
lt_target2 = CORRESPONDING #( BASE ( lt_target2 ) lt_int1 EXCEPT value ) .
 
 
 
3.2.5. Use of MAPPING:  When using structure with different name, we can even map the column as per our wish
 
Syntax :
 
MAPPING  <column from target structure> 
                    = <column of source structure> 
( Make sure that the columns are of compatible types )


lt_target2 = CORRESPONDING #( BASE ( lt_target2 ) lt_int1 MAPPING name = value ) . 
 
* As a result,  row will have same value for columns 'name' and 'value'
 
  
 
3.2.6. Move corresponding with deep table
 
We can directly use move corresponding to transfer data from deep table also : 
Lets create below two  deep table as below :

TYPES : BEGIN OF ty_int2,
          name TYPE char10,
          value TYPE char20,
          END OF ty_int2,
          tt_int2 TYPE STANDARD TABLE OF ty_int2 WITH DEFAULT KEY,
          BEGIN OF ty_deep,
           region TYPE char5,
           country TYPE char10,
           ls_work_info TYPE tt_int2,
           END OF ty_deep,
           tt_deep TYPE STANDARD TABLE OF ty_deep WITH DEFAULT KEY.


DATA(lt_deep) = VALUE tt_deep(
                               (
                                 region = 'APJ' country = 'INDIA'
                                 ls_work_info = VALUE #(
                                            ( name = 'TECH' value = 'SAP' )
                                             ( name = 'DOMAIN' value = 'ABAP' )
                                                       )
                                )   " first row
                               (
                                 region = 'EMEA' country = 'UK'
                                 ls_work_info = VALUE #(
                                            ( name = 'TECH' value = 'ML' )
                                               ( name = 'DOMAIN' value = 'PYTHON' )
                                                       )
                                ) " second row
                              ).

 

DATA(lt_deep2) = VALUE tt_deep(
                               (
                                 region = 'APJ2' country = 'INDIA2'
                                 ls_work_info = VALUE #(
                                       ( name = 'TECH2' value = 'SAP2' )
                                         ( name = 'DOMAIN2' value = 'ABAP2' )
                                                       )
                                )   " first row
                               (
                                 region = 'EMEA2' country = 'UK2'
                                 ls_work_info = VALUE #(
                                               ( name = 'TECH2' value = 'ML2' )
                                              ( name = 'DOMAIN2' value = 'PYTHON2' )
                                                       )
                                ) " second row
                              ).

·   

Original values of tables : 
 

  • 'Expanding nested tables' is used to expand deep structure and 'Keeping target lines' is used so that internal table data from lt_deep will not overwrite data of lt_deep2 but data will get appended. 
MOVE-CORRESPONDING lt_deep TO lt_deep2  EXPANDING NESTED TABLES  
                                                                                   KEEPING TARGET LINES.
 
After executing the above code , it will simply get appended : 


 

 Next - Filter

 


 



No comments:

Post a Comment