AS ABAP Release 758, ©Copyright 2024 SAP SE. All rights reserved.
ABAP - Keyword Documentation → ABAP - Programming Language → Processing Internal Data → Assignments → Assigning Structure Components → CORRESPONDING, Component Operator →CORRESPONDING, Basic Form
Syntax
... { CORRESPONDING {dtype|#}( [EXACT] [DEEP]
struct|{itab [
duplicates]} ) }
| { CORRESPONDING {dtype|#}( [DEEP]
[[APPENDING] BASE ( base )]
struct|{itab [
duplicates]} ) }
| { CORRESPONDING {dtype|#}( [[APPENDING] BASE ( base )]
struct|{itab [
duplicates]}
mapping ) } ...
1. ... EXACT ...
2. ... DEEP ...
3. ... BASE ( base ) ...
4. ... APPENDING ( base )
5. ... DEEP APPENDING ( base )
Effect
This variant of the component operator CORRESPONDING constructs a result with the target type specified using dtype or # from the components of a parameter struct or itab. struct and itab are general expression positions.
Hints
struct2 = CORRESPONDING #( struct1 ).
without the addition BASE is not the same as an assignment
MOVE-CORRESPONDING struct1 TO struct2.
In MOVE-CORRESPONDING, all not identically named components in struct2 keep their value. When the result of the constructor expression is assigned, however, they are assigned the value from there, which is initial for ignored components. This behavior can be overridden using the addition BASE.
dobj = CORRESPONDING type( ... ).
the target object is used directly for optimization reasons and then assigned. A temporary version of the expression is not created. This is not possible when the target object and parameter are identical, and a mapping rule is used, which enables, for example, the swapping of the content of two components. In cases like this, a temporary copy of the target object must be created, which is then used and a corresponding syntax warning is produced. This warning can be hidden using a pragma. If this is not detected until runtime, the information needed to create the necessary temporary copy of the target object is missing and the runtime error CORRESPONDING_SELF occurs.
Example
Assignment of four identically named components of standard table spfli_tab to a temporarily sorted table of type flights in an operand position.
TYPES:
BEGIN OF flight,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto,
END OF flight.
TYPES
flights TYPE SORTED TABLE OF flight WITH UNIQUE KEY carrid connid.
SELECT *
FROM spfli
INTO TABLE @FINAL(spfli_tab).
cl_demo_output=>display( CORRESPONDING flights( spfli_tab ) ).
... EXACT ...
Effect
If the addition EXACT is specified for a structure struct, the assignment is made as with the addition EXACT of the statement MOVE-CORRESPONDING for structures. If the addition EXACT is specified for an internal table itab, the assignment is made in accordance with the rules outlined for the statement MOVE-CORRESPONDING for internal tables.
Example
Assignment of results of the component operator without and with addition EXACT to existing structures of the same content. The example with EXACT demonstrates that, 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.
DATA:
BEGIN OF src,
a TYPE string VALUE `AAA`,
b TYPE string VALUE `BBB`,
c TYPE string VALUE `CCC`,
END OF src,
BEGIN OF target1,
a TYPE string,
b TYPE c LENGTH 1,
c TYPE string,
d TYPE i,
END OF target1.
DATA(target2) = target1.
target1 = CORRESPONDING #( src ).
TRY.
target2 = CORRESPONDING #( EXACT src ).
CATCH cx_root INTO FINAL(error).
ENDTRY.
cl_demo_output=>new( )->write( target1 )->display( target2 ).
... DEEP ...
Effect
If the addition DEEP is specified, the assignment is made as with the addition EXPANDING NESTED TABLES of the statement MOVE-CORRESPONDING. A mapping rule mapping can be used to override the matching name assignment rule of MOVE-CORRESPONDING. If a mapping rule is specified, the addition DEEP is set implicitly and cannot be specified explicitly.
Hint
If the additions EXACT and DEEP are specified, the order of the additions is irrelevant, i. e. both ... EXACT DEEP ... and ... DEEP EXACT ... are possible. Furthermore, the only syntax option using both DEEP and EXACT with more additions is DISCARDING DUPLICATES.
Example
Assignment of results of the component operator with the additions DEEP and EXACT DEEP to existing deep structures of the same content.
DATA:
BEGIN OF struc1,
a TYPE string,
b TYPE string,
c TYPE i,
END OF struc1,
BEGIN OF struc2,
a TYPE string,
b TYPE c LENGTH 1,
c TYPE i,
END OF struc2,
BEGIN OF str_deep1,
comp TYPE string VALUE `ZZZ`,
itab LIKE TABLE OF struc1 WITH EMPTY KEY,
END OF str_deep1,
BEGIN OF str_deep2,
comp TYPE string,
itab LIKE TABLE OF struc2 WITH EMPTY KEY,
END OF str_deep2.
str_deep1-itab = VALUE #( ( a = `AAA` b = `BBB` c = 111 ) ).
DATA(str_deep3) = str_deep2.
str_deep2 = CORRESPONDING #( DEEP str_deep1 ).
TRY.
str_deep3 = CORRESPONDING #( EXACT DEEP str_deep1 ).
CATCH cx_root INTO FINAL(error).
ENDTRY.
cl_demo_output=>new( )->write( str_deep2 )->display( str_deep3 ).
... BASE ( base ) ...
Effect
The addition BASE can be used to specify a start value base for the created structure or internal table. base is a functional operand position in which a database convertible to the target type can be specified.
If the addition BASE is specified, the value of base is assigned to the target structure or target table in accordance with the general assignment rules before the remainder of the expression is evaluated. Here, the addition duplicates after itab can be used to affect the behavior with respect to duplicate lines occurring in a target table.
Hints
struct2 = CORRESPONDING #( BASE ( struct2 ) struct1 ).
is the same as an assignment
MOVE-CORRESPONDING struct1 TO struct2.
itab2 = CORRESPONDING #( BASE ( itab2 ) itab1 ).
is the same as an assignment
MOVE-CORRESPONDING itab1 TO itab2 KEEPING TARGET LINES.
Example
Assignment of results of the component operator without and with addition BASE to existing structures of the same content. The value of the component that does not exist in the source structure is only preserved if BASE is used.
DATA:
BEGIN OF src,
a TYPE i VALUE 1,
b TYPE i VALUE 2,
END OF src,
BEGIN OF target1,
b TYPE i VALUE 11,
c TYPE i VALUE 12,
END OF target1.
DATA(target2) = target1.
target1 = CORRESPONDING #( src ).
target2 = CORRESPONDING #( BASE ( target2 ) src ).
cl_demo_output=>new( )->write( target1 )->display( target2 ).
... APPENDING ( base ) ...
Effect
This addition affects two identically named deep components that are both internal tables. It ensures that the nested target tables are not deleted. Furthermore, it appends the lines of the nested source tables. The value assignment applied to the added lines is performed in the same way as using the CORRESPONDING operator without addition.
Note: The entire table bodies of the nested source tables are assigned in accordance with the conversion rules for internal tables.
Hint
The addition APPENDING appends the lines of a nested table and keeps the original lines. The explicit specification of the addition BASE is possible here but not necessary. That means, the following syntax has the same effect:
Example
The following source code section taken from CL_DEMO_CRRSPNDNG_VARIANTS_ST demonstrates the variant with deep structures containing internal tables as components.
... DEEP APPENDING ( base ) ...
Effect
This combination affects two identically named components that are both internal tables. It ensures that the nested target tables are not deleted. Furthermore, it appends the lines of the nested source tables. The value assignment applied to the added lines is performed in the same way as using the CORRESPONDING operator with the addition DEEP. That is, the value assignment is only carried out for identically named components in the nested table.
Hint
The addition APPENDING appends the lines of a nested table and keeps the original lines. The explicit specification of the addition BASE is possible here but not necessary. That means, the following syntax has the same effect:
Example
The following source code section taken from CL_DEMO_CRRSPNDNG_VARIANTS_ST demonstrates the variant with deep structures containing internal tables as components.
Executable Examples