合并两个 itab 之间深层结构中的嵌套表

Merge nested tables in deep structure between two itabs

我需要一些帮助来找到合并一些嵌套 table 的最快和最简单的方法。 例如:

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.

现在我想在 ITAB2 中添加从 ITAB 到 ITAB2、WA-SFLIGHT1 到 WA2-SFLIGHT2 和 WA-MARA1 到 WA2-MARA2 的行,而不在 ITAB2 中创建新行。

例如: ITAB 有 1 条线,WA-SFLIGHT1 有 3 条线,WA-MARA1 有 6 条线。 ITAB2 有 1 条线,WA2-SFLIGHT2 有 6 条线,WA2-MARA2 有 6 条线。 现在我想将 WA-SFLIGHT1 的 3 行和 WA-MARA1 的 6 行从 ITAB 附加到 WA2-SFLIGHT2 并将 WA2-MARA2 附加到 ITAB2。最后,ITAB2 有 1 条线,WA2-SFLIGHT2 有 9 条线(3 条来自 ITAB),WA2-MARA2 有 12 条线(6 条来自 ITAB)。

它应该是动态的,因为在我的例子中,我有一个包含 6 table 的深层结构,我需要将这些行附加到 ITAB 中的新结构,而无需仅在 ITAB 本身中创建新行在结构中-table.

非常感谢。

    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.