The CORRESPONDING operator copies data between structures or tables, where fields with matching names are automatically mapped. It is the modern alternative to the MOVE-CORRESPONDING statement.
Syntax
CORRESPONDING <type>( <source> [ BASE ( <base> ) ] [ MAPPING <target1> = <source1> ... ] [ EXCEPT <field1> <field2> ... ])Basic Principle
Fields are mapped by name:
- Fields with the same name are copied
- Fields without a match remain unchanged (or initial)
- Different types are converted (if compatible)
Examples
1. Simple Structure Mapping
TYPES: BEGIN OF ty_source, id TYPE i, name TYPE string, email TYPE string, END OF ty_source,
BEGIN OF ty_target, id TYPE i, name TYPE string, address TYPE string, END OF ty_target.
DATA: ls_source TYPE ty_source, ls_target TYPE ty_target.
" Fields with same names are copiedls_target = CORRESPONDING #( ls_source ).
" ls_target-id = 1" ls_target-name = 'Max'" ls_target-address = '' (not present in source)" email is ignored (not present in target)2. CORRESPONDING vs. MOVE-CORRESPONDING
" ClassicMOVE-CORRESPONDING ls_source TO ls_target.
" Modern (functionally equivalent)ls_target = CORRESPONDING #( ls_source ).
" The difference: CORRESPONDING is an expression" and can be used in other expressionsDATA(ls_result) = CORRESPONDING ty_target( ls_source ).3. BASE – Preserve Existing Values
Without BASE, unmapped fields are set to initial value:
ls_target = VALUE #( id = 99 name = 'Old' address = 'Berlin' ).
" WITHOUT BASE: address becomes initialls_target = CORRESPONDING #( ls_source )." ls_target-address = ''
" WITH BASE: address is preservedls_target = VALUE #( id = 99 name = 'Old' address = 'Berlin' ).ls_target = CORRESPONDING #( BASE ( ls_target ) ls_source )." ls_target-address = 'Berlin'4. MAPPING – Rename Fields
When source and target fields have different names:
TYPES: BEGIN OF ty_db_record, kunnr TYPE kunnr, name1 TYPE string, ort01 TYPE string, END OF ty_db_record,
BEGIN OF ty_customer, customer_id TYPE kunnr, customer_name TYPE string, city TYPE string, END OF ty_customer.
DATA: ls_db_record TYPE ty_db_record, ls_customer TYPE ty_customer.
ls_db_record = VALUE #( kunnr = '1000' name1 = 'Miller Inc.' ort01 = 'Berlin' ).
" With MAPPING: Map field namesls_customer = CORRESPONDING #( ls_db_record MAPPING customer_id = kunnr customer_name = name1 city = ort01).
" ls_customer-customer_id = '1000'" ls_customer-customer_name = 'Miller Inc.'" ls_customer-city = 'Berlin'5. EXCEPT – Exclude Fields
TYPES: BEGIN OF ty_user, id TYPE i, name TYPE string, password TYPE string, email TYPE string, END OF ty_user.
DATA: ls_user_full TYPE ty_user, ls_user_safe TYPE ty_user.
ls_user_full = VALUE #().
" Do NOT copy passwordls_user_safe = CORRESPONDING #( ls_user_full EXCEPT password ).
" ls_user_safe-password = '' (not copied)6. Combine MAPPING and EXCEPT
TYPES: BEGIN OF ty_order_db, aufnr TYPE string, kdnr TYPE string, datum TYPE d, internal TYPE string, " Internal field END OF ty_order_db,
BEGIN OF ty_order_api, order_id TYPE string, customer_id TYPE string, order_date TYPE d, END OF ty_order_api.
DATA: ls_order_db TYPE ty_order_db, ls_order_api TYPE ty_order_api.
ls_order_db = VALUE #( aufnr = 'ORD001' kdnr = 'C100' datum = sy-datum internal = 'XYZ').
ls_order_api = CORRESPONDING #( ls_order_db MAPPING order_id = aufnr customer_id = kdnr order_date = datum EXCEPT internal).7. Transform Tables
TYPES: ty_source_tab TYPE TABLE OF ty_source WITH EMPTY KEY, ty_target_tab TYPE TABLE OF ty_target WITH EMPTY KEY.
DATA: lt_source TYPE ty_source_tab, lt_target TYPE ty_target_tab.
lt_source = VALUE #().
" Transform entire tablelt_target = CORRESPONDING #( lt_source ).
" With MAPPINGlt_target = CORRESPONDING #( lt_source MAPPING id = id name = name).8. DEEP – Deep Structures
For nested structures with DEEP:
TYPES: BEGIN OF ty_address, street TYPE string, city TYPE string, END OF ty_address,
BEGIN OF ty_person_full, name TYPE string, address TYPE ty_address, items TYPE TABLE OF string WITH EMPTY KEY, END OF ty_person_full.
DATA: ls_source TYPE ty_person_full, ls_target TYPE ty_person_full.
ls_source = VALUE #( name = 'Max' address = VALUE #( street = 'Main St.' city = 'Berlin' ) items = VALUE #( ( `Item1` ) ( `Item2` ) )).
" DEEP also copies nested structures and tablesls_target = CORRESPONDING #( DEEP ls_source ).9. CORRESPONDING in Method Parameters
METHODS: process_order IMPORTING is_api_order TYPE ty_order_api.
" Call with transformationprocess_order( is_api_order = CORRESPONDING #( ls_db_order MAPPING order_id = aufnr customer_id = kdnr )).10. Combine with VALUE
" Create structure and transform simultaneouslyDATA(ls_output) = VALUE ty_order_api( BASE CORRESPONDING #( ls_db_order MAPPING order_id = aufnr ) " Set additional fields status = 'NEW').11. Filter and Transform Table
" Combine with FOR and CORRESPONDINGDATA(lt_active_customers) = VALUE ty_customers_api( FOR ls_db IN lt_customers_db WHERE ( status = 'A' ) ( CORRESPONDING #( ls_db MAPPING customer_id = kunnr customer_name = name1 ) )).CORRESPONDING vs. MOVE-CORRESPONDING
| Aspect | CORRESPONDING #() | MOVE-CORRESPONDING |
|---|---|---|
| Type | Expression (returns value) | Statement |
| Inline usable | Yes | No |
| BASE | Yes | No (always overwrites) |
| MAPPING | Yes | No |
| EXCEPT | Yes | No |
| DEEP | Yes | Limited |
| Recommendation | Modern, preferred | Legacy |
" MOVE-CORRESPONDING: Only simple mappingMOVE-CORRESPONDING ls_source TO ls_target.
" CORRESPONDING: Flexible and powerfulls_target = CORRESPONDING #( BASE ( ls_target ) ls_source MAPPING target_field = source_field EXCEPT unwanted_field).Important Notes / Best Practice
CORRESPONDING #()is an expression – use it inline in other expressions.- Use
BASEto preserve existing values. MAPPINGenables field renaming between different structures.EXCEPTexcludes sensitive or unnecessary fields.DEEPis necessary for nested structures and tables.- Combine with
VALUEandFORfor complex transformations. - For large data volumes, CORRESPONDING is efficiently implemented.
- Pay attention to type compatibility – incompatible types lead to errors.
- Replace
MOVE-CORRESPONDINGwithCORRESPONDING #()in new code.