0

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.

Mogera
  • 37
  • 1
  • 9
  • 2
    "TAB has 1 line with WA-SFLIGHT1 which has 3 lines and WA-MARA1 which has 6 lines" SFLIGHT1 and MARA1 can't has 3 or 6 lines because it's not tables – alezhu Jan 25 '22 at 07:02
  • Ah my bad sorry. – Mogera Jan 25 '22 at 10:56
  • you may also check [my macros approach](https://stackoverflow.com/a/63175831/911419), if you want smth rather more concise than universal/dynamic, it will fit – Suncatcher Feb 03 '22 at 17:00

1 Answers1

1
    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.

"3.Merge tables of same struct dynamic
FIELD-SYMBOLS <t_data1> TYPE ANY TABLE.
FIELD-SYMBOLS <t_data2> TYPE ANY TABLE.
DO.
  ASSIGN COMPONENT sy-index OF STRUCTURE ls_test1 TO FIELD-SYMBOL(<any1>).
  IF sy-subrc NE 0.
    EXIT.
  ENDIF.
  DESCRIBE FIELD <any1> TYPE DATA(lv_type).
  CHECK lv_type = 'h'. "Table

  ASSIGN <any1> TO <t_data1>.
  ASSIGN COMPONENT sy-index OF STRUCTURE ls_test2 TO <t_data2>.

  INSERT LINES OF <t_data1> INTO TABLE <t_data2>.
ENDDO.
BREAK-POINT.

"4. Merge tables of corresponding struct dynamic
DATA(lt_comp) = CAST cl_abap_structdescr( cl_abap_structdescr=>describe_by_data( ls_test1 ) )->get_included_view( ).
LOOP AT lt_comp REFERENCE INTO DATA(lps_comp)
  WHERE type->kind = cl_abap_structdescr=>kind_table.

  ASSIGN COMPONENT lps_comp->name of STRUCTURE ls_test2 to <t_data2>.
  CHECK sy-subrc = 0.

  ASSIGN COMPONENT lps_comp->name of STRUCTURE ls_test1 to <t_data1>.
  CHECK sy-subrc = 0.

  INSERT LINES OF <t_data1> INTO TABLE <t_data2>.
ENDLOOP.
BREAK-POINT.
alezhu
  • 475
  • 3
  • 6
  • Thanks a lot. Thats exactly what I needed. – Mogera Jan 25 '22 at 10:56
  • Is there a way to dynamicly append lines from one strcuture to another structure ? For Example: Loop at components of wa. APPEND LINES OF wa fields to corresponding wa2 fields. Endloop. So it should be like "1.Merge struct but without knowing the fields in structure. – Mogera Jan 25 '22 at 12:04
  • 1
    "Is there a way to dynamicly append lines from one strcuture to another structure ?" Yes. I added method 3 and 4 for that. Method 3 is for structure of same type. Method 4 is for different structures with same fields with same types in it. – alezhu Jan 26 '22 at 08:57
  • You are the one! I wish you all the best in the world. This ist just wonderful, thank you! – Mogera Jan 27 '22 at 00:13