ABAP - Keyword Documentation →  ABAP - Programming Language →  Processing Internal Data →  Assignments →  Assigning Structure Components → 
Mail Feedback

MOVE-CORRESPONDING

Short Reference

Syntax Forms

Variant for Structures

1. MOVE-CORRESPONDING [EXACT] struc1 TO struc2
    [EXPANDING NESTED TABLES] [KEEPING TARGET LINES].

Variant for Internal Tables

2. MOVE-CORRESPONDING [EXACT] itab1 TO itab2
    [EXPANDING NESTED TABLES] [KEEPING TARGET LINES].


Variants:

1. MOVE-CORRESPONDING struc1 TO struc2.

2. MOVE-CORRESPONDING itab1 TO itab2.

Additions:

1. ... EXACT

2. ... EXPANDING NESTED TABLES

3. ... KEEPING TARGET LINES

Effect

The statement MOVE-CORRESPONDING is used to assign identically named components of structured data objects to each other. There are two variants of the statements:

No other combinations of operand types are possible. Field symbols typed with the generic type ANY or formal parameters can also be used as operands. An operand of this type must be either a structure or an internal table when the statement is executed and match the other operands, otherwise an uncatchable exception is raised. struc1 and itab1 are functional operand positions.

See the RAP-related variant here: to do ...

Hints



Variant 1  

MOVE-CORRESPONDING struc1 TO struc2.


Effect

This variant of the statement MOVE-CORRESPONDING requires structures to be specified for struc1 and struc2. As operands of the statement MOVE-CORRESPONDING, meshes are handled like regular structures and can also be specified.

The system searches for all identically named components in struc1 and struc2 and the content of components in struc1 is assigned to the identically named components in struc2. Other components are not affected. If field symbols are used as operands, the names of the components are evaluated in accordance with the data type of the field symbols, which can be distinguished by casting the names of the actual structures.

Nested structures are fully resolved. The names of the components are compared down to the lowest common level. If the addition EXPANDING NESTED TABLES is not specified, the following statement is executed identically for each named component pair comp:

struc2-comp = struc1-comp.

Any associated conversions are performed, and the corresponding exceptions may be raised. In particular, if the components are table-like, the entire table body is assigned in accordance with the conversion rules for internal tables.

If struc1 or struc2 are empty customizing includes when the statement is executed (that is they do not contain any components), the statement is ignored. If struc1 is a structure that contains empty customizing includes as components, these are also ignored when the structure is evaluated.

Hints

Example

This example shows how MOVE-CORRESPONDING is applied to two structures with the same type t_str and which are cast using field symbols with different types. The statement evaluates the names of the types of the field symbols, whereby the content of components that actually have different names are assigned.

TYPES: BEGIN OF t_str,
         a1 TYPE i,
         a2 TYPE i,
       END OF t_str.

TYPES: BEGIN OF t_str1,
         b1 TYPE i,
         b2 TYPE i,
       END OF t_str1.

TYPES: BEGIN OF t_str2,
         b2 TYPE i,
         b1 TYPE i,
       END OF t_str2.

FINAL(str1) = VALUE t_str( a1 = 1 a2 = 2 ).
DATA str2 LIKE str1.

FIELD-SYMBOLS <fs1> TYPE t_str1.
ASSIGN str1 TO <fs1> CASTING.

FIELD-SYMBOLS <fs2> TYPE t_str2.
ASSIGN str2 TO <fs2> CASTING.

MOVE-CORRESPONDING <fs1> TO <fs2>.

cl_demo_output=>write(   str1 ).
cl_demo_output=>display( str2 ).

Variant 2  

MOVE-CORRESPONDING itab1 TO itab2.


Effect

This variant of the statement MOVE-CORRESPONDING requires internal tables to be specified for itab1 and itab2. It searches for all identically named components in the line types of itab1 and itab2 and assigns them from itab1 to itab2 in accordance with the rules below.

If there are identically named components, the target table itab2 is deleted without the addition KEEPING TARGET LINES and the same number of initial lines are inserted as exist in the source table itab1. The lines of the source table are then read sequentially in the same order as in the statement LOOP, and the content of each line is assigned to the corresponding line in the target table in accordance with the rules for using EXACT with structures. Finally, and if necessary, the table keys and associated table indexes are updated in the target table in accordance with the rules for insertions in internal tables. The corresponding exceptions are raised if uniqueness is violated.

If there are no identically named components, no assignment is made and the target table remains unchanged.

Hints

Example

Assignment of four identically named components of standard table spfli_tab to a sorted table flights.

TYPES:
  BEGIN OF flight,
    carrid   TYPE spfli-carrid,
    connid   TYPE spfli-connid,
    cityfrom TYPE spfli-cityfrom,
    cityto   TYPE spfli-cityto,
  END OF flight.
DATA
  flights TYPE SORTED TABLE OF flight WITH UNIQUE KEY carrid connid.

SELECT *
       FROM spfli
       INTO TABLE @FINAL(spfli_tab).

MOVE-CORRESPONDING spfli_tab TO flights.

cl_demo_output=>display( flights ).

Addition 1  

... EXACT

Effect

Variant for Structures

If the addition EXACT is specified for MOVE-CORRESPONDING, the following lossless assignment is performed for each identically named component pair comp

struc2-comp = EXACT #( struc1-comp ).

and the corresponding checks are performed. If an exception is raised, all components are assigned up to the component that raised the exception. This component, and all following components, are not assigned.

Variant for Internal Tables

If the source table contains valid values in all components of the internal table line and no values are lost, the conversion is performed in accordance with the associated conversion rules. This check is performed line-wise beginning with the first line of the internal table. If at least one component of the line causes a conversion error, the whole line is not assigned and none of the following lines either. In that case, initial lines are added to the target table.

Example

The following example demonstrates assignments with MOVE-CORRESPONDING EXACT and internal tables. One of the internal tables that is filled for demonstration purposes before the assignment purposely includes a component value (component b has a value including three characters which is not compatible with ... b TYPE c LENGTH 2 ...) for which a lossless assignment fails. The output shows that up to this line, the assignment works. For the table line for which the assignment error happens and all of the following lines, no assignments are made. There are only initial lines added to the target table.

TYPES:
  BEGIN OF struc1,
    a TYPE c LENGTH 3,
    b TYPE c LENGTH 3,
    c TYPE i,
  END OF struc1,

  BEGIN OF struc2,
    a TYPE c LENGTH 2,
    b TYPE c LENGTH 2,
    c TYPE i,
  END OF struc2,

  BEGIN OF struc3,
    a TYPE c LENGTH 3,
    b TYPE c LENGTH 3,
    c TYPE i,
  END OF struc3.

DATA itab1 TYPE TABLE OF struc1 WITH EMPTY KEY.
DATA itab2 TYPE TABLE OF struc2 WITH EMPTY KEY.
DATA itab3 TYPE TABLE OF struc3 WITH EMPTY KEY.

itab1 = VALUE #( ( a = `aa` b = `bb` c = 123 )
                 ( a = `cc` b = `ddd` c = 456 )
                 ( a = `ee` b = `ff` c = 789 ) ).

TRY.
    MOVE-CORRESPONDING EXACT itab1 TO itab2.
  CATCH cx_sy_conversion_data_loss INTO DATA(error).
ENDTRY.

IF error IS NOT INITIAL.
  cl_demo_output=>write( error->get_text( ) ).
ENDIF.

cl_demo_output=>write( itab2 ).

TRY.
    MOVE-CORRESPONDING EXACT itab1 TO itab3.
  CATCH cx_sy_conversion_data_loss INTO DATA(error2).
ENDTRY.

IF error2 IS NOT INITIAL.
  cl_demo_output=>write( error2->get_text( ) ).
ENDIF.

cl_demo_output=>write( itab3 ).

cl_demo_output=>display(  ).

Addition 2  

... EXPANDING NESTED TABLES

Effect

Variant for Structures

With this addition, two identically named components, that are both internal tables, are not simply assigned. Instead, for these components, the statement MOVE-CORRESPONDING [EXACT] for internal tables with the addition EXPANDING NESTED TABLES and without the addition KEEPING TARGET LINES is executed.

Tabular components are resolved at every hierarchy level and identically named components are assigned line by line. The target tables are deleted before an assignment.

Hint

If one of two identically named components is an internal table and the other is not, MOVE-CORRESPONDING is never possible, regardless of whether EXPANDING NESTED TABLES is used.

Example

In the following example, the structure struc1 contains the components:

The structure struc2 contains the components:

Over the length of the shorter path, the components struci-comp1, struci-comp2, and itab have the same name. These are assigned from struc1 to struc2 in both MOVE-CORRESPONDING statements. In struc1, struci-comp2 is self-structured; in struc2, struci-comp2 is elementary. When struc1-struci-comp2 is assigned to struc2-struci-comp2, the source field is interpreted as an elementary field of type c in accordance with the conversion rules for structures.

The components itab are table-like and have compatible line types. The statement MOVE-CORRESPONDING without the addition EXPANDING NESTED TABLE is used to assign the table body and the content of itab in struc2 then matches the content of itab in struc1. If the addition EXPANDING NESTED TABLE is used, only the component col2 is assigned and col3 remains initial.

The components struc1-comp1 and struc2-struci-comp3 do not have any equivalents with the same name and are ignored in the assignment.

TYPES: BEGIN OF line1,
         col1 TYPE i,
         col2 TYPE i,
       END OF line1,
       BEGIN OF line2,
         col2 TYPE i,
         col3 TYPE i,
       END OF line2.

DATA: BEGIN OF struc1,
        comp1 TYPE c LENGTH 1 VALUE 'U',
        BEGIN OF struci,
          comp1 TYPE c LENGTH 1 VALUE 'V',
          BEGIN OF comp2,
            col1 TYPE c LENGTH 1 VALUE 'X',
            col2 TYPE c LENGTH 1 VALUE 'Y',
          END OF comp2,
        END OF struci,
        itab TYPE TABLE OF line1 WITH EMPTY KEY,
     END OF struc1.

DATA: BEGIN OF struc2,
        BEGIN OF struci,
          comp1 TYPE string,
          comp2 TYPE string,
          comp3 TYPE string,
        END OF struci,
        itab TYPE TABLE OF line2 WITH EMPTY KEY,
     END OF struc2.

struc1-itab = VALUE #(
  ( col1 = 11 col2 = 12 )
  ( col1 = 21 col2 = 22 ) ).

MOVE-CORRESPONDING struc1 TO struc2.
MOVE-CORRESPONDING struc1 TO struc2 EXPANDING NESTED TABLES.

Variant for Internal Tables

If this addition is specified, the individual lines are assigned in accordance with the rules for MOVE-CORRESPONDING [EXACT] with EXPANDING NESTED TABLES specified, and tabular components are resolved at every hierarchy level.

If the addition is not specified, the individual lines are assigned in accordance with the rules for MOVE-CORRESPONDING [EXACT] without EXPANDING NESTED TABLES specified, and tabular components are assigned in accordance with the rules for assignments or lossless assignments.

Addition 3  

... KEEPING TARGET LINES

Effect

Variant for Structures

This addition affects two identically named components that are both internal tables. It ensures that the nested target tables in struc2 are not deleted. Furthermore, it appends the lines of the nested source tables in struc1 by executing the statement MOVE-CORRESPONDING [EXACT] for internal tables without an addition.

Hints



Variant for Internal Tables

This addition ensures that the target table itab2 is not deleted. Instead, it appends the same number of initial lines as exist in the source table itab1 and assigns the lines of the source table to these initial lines. The table keys and indexes are then updated across all lines. If no identically named components are found, the target table remains unchanged.

Hint

The addition KEEPING TARGET LINES is only effective on the lines of itab2. It cannot be effective on nested tables, even if it is specified with the addition EXPANDING NESTED TABLES. This is because nested tables are always resolved in new initial lines.

Uncatchable Exceptions

Executable Examples