I have two internal tables with a content: itab1
, itab2
and one final table ftab
. I need to move the contents from itab1
and itab2
to ftab
with corresponding values.
For example, move the first record of itab1
to the corresponging first record of ftab
and also the first record of itab2
to the corresponding first record of ftab
keeping all the values from both tables. Basically it means to merge the contents of the first records of both tables and put it into the final table.
I can do it by using ftab = CORRESPONDING #( itab1 )
, subsequent looping at ftab
, reading itab2
and filling the remaining columns.
I want to know - is there any way to do it without using loop?
Actually there is no possibility to merge as described 2 tables into a third one without looping. Using CORRESPONDING
it is possible just to add second table's content after the content of the first one.
What is actually possible is to make the looping and filling the target table in one statemet using iteration expression FOR
and component operator CORRESPONDING
. Below I wrote an example code for this.
Data declarations:
TYPES:
BEGIN OF ty_itab1,
matnr_1 TYPE mara-matnr,
text_1 TYPE mara-zeinr,
text_2 TYPE mara-ferth,
text_3 TYPE mara-ferth,
END OF ty_itab1,
BEGIN OF ty_itab2,
descr_1 TYPE mara-zeinr,
descr_2 TYPE mara-ferth,
descr_3 TYPE mara-ferth,
END OF ty_itab2,
BEGIN OF ty_ftab.
INCLUDE TYPE ty_itab1.
INCLUDE TYPE ty_itab2.
TYPES: zfield TYPE mara-ferth,
END OF ty_ftab,
ty_t_itab1 TYPE STANDARD TABLE OF ty_itab1 WITH DEFAULT KEY,
ty_t_itab2 TYPE STANDARD TABLE OF ty_itab2 WITH DEFAULT KEY,
ty_t_ftab TYPE STANDARD TABLE OF ty_ftab WITH DEFAULT KEY.
Fill in some sample data:
DATA(lt_itab1) = VALUE ty_t_itab1(
( matnr_1 = '123MAT1' text_1 = 'Text 1' text_2 = 'Note 1' text_3 = 'Data 1' )
( matnr_1 = '123MAT2' text_1 = 'Text 2' text_2 = 'Note 2' text_3 = 'Data 2' )
( matnr_1 = '123MAT3' text_1 = 'Text 3' text_2 = 'Note 3' text_3 = 'Data 3' )
( matnr_1 = '123MAT4' text_1 = 'Text 4' text_2 = 'Note 4' text_3 = 'Data 4' ) ).
DATA(lt_itab2) = VALUE ty_t_itab2(
( descr_1 = 'Description 1' descr_2 = 'Info 1' descr_3 = 'Map 1')
( descr_1 = 'Description 2' descr_2 = 'Info 2' descr_3 = 'Map 2')
( descr_1 = 'Description 3' descr_2 = 'Info 3' descr_3 = 'Map 3' ) ).
Build a target table lt_ftab
which is a merge of the two above:
DATA(lt_ftab) = VALUE ty_t_ftab(
FOR ls_itab1 IN lt_itab1 INDEX INTO lv_index
LET ls_ftab = VALUE ty_ftab(
descr_1 = VALUE #( lt_itab2[ lv_index ]-descr_1 OPTIONAL )
descr_2 = VALUE #( lt_itab2[ lv_index ]-descr_2 OPTIONAL )
zfield = VALUE #( lt_itab2[ lv_index ]-descr_3 OPTIONAL )
)
IN ( CORRESPONDING #( BASE ( ls_ftab ) ls_itab1 ) ) ).
If you use this statement instead:
DATA(lt_ftab) = CORRESPONDING ty_t_ftab( BASE ( lt_itab1 ) lt_itab2
MAPPING zfield = descr_3 ).
the content of two tables will be just added together but not merged.