ABAP-Dynamic-Internal table

data:
      lr_any_t  type ref to data,
      lr_any_s  type ref to data.
    data:
      lr_dref   type ref to data,
      lr_stru   type ref to cl_abap_structdescr,
      lr_table  type ref to cl_abap_tabledescr,
      lr_elem   type ref to cl_abap_elemdescr,
      lt_para   type cl_abap_structdescr=>component_table,
      lt_comp   type cl_abap_structdescr=>component_table.
    field-symbols:
      <fs_tab>  type any,
      <ft_tab>  type standard table.

    try.

      lr_stru ?= cl_abap_tabledescr=>describe_by_data( it_any ).

      loop at lr_stru->components[] into data(ls_comps).
        if ls_comps-type_kind = 'P'
        or ls_comps-type_kind = 'X'.
        else.
          ls_comps-length = ls_comps-length / 2.
        endif.

        append initial line to lt_comp assigning field-symbol(<fs_comp>).
        <fs_comp>-name  =   ls_comps-name.
        <fs_comp>-type  ?=  cl_abap_elemdescr=>get_by_kind(
                            p_type_kind = ls_comps-type_kind
                            p_length = ls_comps-length
                            p_decimals = ls_comps-decimals ).
        unassign <fs_comp>.
      endloop.

      if lt_comp[] is not initial.
        clear:lr_stru,lr_table.
        lr_stru   =   cl_abap_structdescr=>create( lt_comp ).
        lr_table  =   cl_abap_tabledescr=>create( lr_stru ).

        create data lr_any_s type handle lr_stru.
        assign lr_any_s->* to <fs_tab>.

        create data lr_any_t type handle lr_table.
        assign lr_any_t->* to <ft_tab>.
      endif.

      move-corresponding it_any to <ft_tab>[].

      catch cx_root into data(lr_rt).
      data(lv_txt) = lr_rt->get_text( ).
      raise exception type zcx_root
        exporting
          textid = value #(
                     msgid = '00'
                     msgno = '001'
                     attr1 = lv_txt
                     ).
    endtry.
View Code

 

posted @ 2021-06-30 17:04  真的是很难  阅读(53)  评论(0编辑  收藏  举报