Home > OS >  Merge nested tables in deep structure between two itabs
Merge nested tables in deep structure between two itabs

Time:01-26

I need some help to find the fastest and easiest way to merge some nested tables. For Example:

TYPES: BEGIN OF TEST,
        SFLIGHT1 TYPE SFLIGHT,
        MARA1    TYPE MARA,
        END OF TEST.

DATA: ITAB TYPE TABLE OF TEST,
      WA   TYPE TEST.
DATA: ITAB2 TYPE TABLE OF TEST,
      WA2   TYPE TEST.
DATA: LT_SFLIGHT1 TYPE SFLIGHT,
      LT_SFLIGHT2 TYPE SFLIGHT.
DATA: LT_MARA1 TYPE MARA,
      LT_MARA2 TYPE MARA.

WA-SFLIGHT1 = LT_SFLIGHT1.
WA-MARA1 = LT_MARA1.
APPEND WA TO ITAB.

WA2-SFLIGHT2 = LT_SFLIGHT2.
WA2-MARA2 = LT_MARA2.
APPEND WA2 TO ITAB2.

Now I want to append lines of from ITAB to ITAB2, WA-SFLIGHT1 to WA2-SFLIGHT2 and WA-MARA1 to WA2-MARA2 without creating a new line in ITAB2.

For Example: ITAB has 1 line with WA-SFLIGHT1 which has 3 lines and WA-MARA1 which has 6 lines. ITAB2 has 1 line with WA2-SFLIGHT2 which has 6 lines and WA2-MARA2 which has 6 lines. Now I want to append the 3 lines of WA-SFLIGHT1 and the 6 lines of WA-MARA1 from ITAB to WA2-SFLIGHT2 and WA2-MARA2 into ITAB2. At the end ITAB2 has 1 line with WA2-SFLIGHT2 with 9 lines (3 from ITAB) and WA2-MARA2 with 12 lines (6 from ITAB).

It should be something dynamic because in my case, I have a deep structure with 6 tables which lines I need to append to a new structure within an ITAB without creating a new line in ITAB itself only in the structure-table.

Thanks a lot.

CodePudding user response:

TYPES tt_sflight TYPE STANDARD TABLE OF sflight WITH EMPTY KEY.
TYPES tt_mara TYPE STANDARD TABLE OF mara WITH EMPTY KEY.
TYPES: BEGIN OF ts_test,
         t_flight TYPE tt_sflight,
         t_mara   TYPE tt_mara,
       END OF ts_test,
       tt_test TYPE STANDARD TABLE OF ts_test WITH EMPTY KEY.

DATA(ls_test1) = VALUE ts_test(
  t_flight = VALUE #(
    ( carrid = 11 )
    ( carrid = 12 )
    ( carrid = 13 )
  )
  t_mara = VALUE #(
    ( matnr = '11' )
    ( matnr = '12' )
    ( matnr = '13' )
  )
).

DATA(ls_test2) = VALUE ts_test(
  t_flight = VALUE #(
    ( carrid = 21 )
    ( carrid = 22 )
    ( carrid = 23 )
  )
  t_mara = VALUE #(
    ( matnr = '21' )
    ( matnr = '22' )
    ( matnr = '23' )
  )
).

DATA(lt_test1) = VALUE tt_test( ( ls_test1 ) ).
DATA(lt_test2) = VALUE tt_test( ( ls_test2 ) ).

"1.Merge struct
APPEND LINES OF ls_test1-t_flight TO ls_test2-t_flight.
APPEND LINES OF ls_test1-t_mara TO ls_test2-t_mara.
BREAK-POINT.

"2.Merge tables
DATA(lps_dst) = REF #( lt_test2[ 1 ] ).
APPEND LINES OF lt_test1[ 1 ]-t_flight TO lps_dst->t_flight.
APPEND LINES OF lt_test1[ 1 ]-t_mara TO lps_dst->t_mara.
BREAK-POINT.
  •  Tags:  
  • Related