Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
  • On this example, I'll create three internal tables into one internal structure created (dynamically).

  • 1st: create three internal tables dynamically.

  • 2st: create one strusture dynamically with the three iTabs created on first pass.

  • 3st: return final structure dynamically created.

DATA:

  l_comp_tab    TYPE cl_abap_structdescr=>component_table,

  l_comp_tab2   TYPE cl_abap_structdescr=>component_table,

  l_comp        TYPE cl_abap_structdescr=>component,

  l_comp2       TYPE cl_abap_structdescr=>component,

  lv_size       TYPE i,  "Field size

  lv_decm       TYPE i,  "Field decimal places.

  lv_tabix      TYPE numc1,  "Field decimal places.

  lv_tab_name   TYPE char10,

  lo_new_tab    TYPE REF TO cl_abap_tabledescr,

  l_estrutura   TYPE REF TO cl_abap_structdescr,

  l_struc_final TYPE REF TO data.

DO 3 TIMES. " Create 3 tables only... Remember: this is just example!

  CLEAR: l_comp , l_comp_tab[].

  ADD 1 TO lv_tabix.

  lv_size = 10.

  l_comp-name = 'FIELD_CHAR'.

  l_comp-type = cl_abap_elemdescr=>get_c( p_length = lv_size ).

  APPEND l_comp TO l_comp_tab. CLEAR l_comp.

  l_comp-name = 'FIELD_INT'.

  l_comp-type = cl_abap_elemdescr=>get_i( ).

  APPEND l_comp TO l_comp_tab. CLEAR l_comp.

  lv_size = 10.

  l_comp-name = 'FIELD_NUM'.

  l_comp-type = cl_abap_elemdescr=>get_n( p_length = lv_size ).

  APPEND l_comp TO l_comp_tab. CLEAR l_comp.

  lv_size = 9.

  lv_decm = 2.

  l_comp-name = 'FIELD_NUMDEC'.

  l_comp-type = cl_abap_elemdescr=>get_p( p_length = lv_size p_decimals = lv_decm ).

  APPEND l_comp TO l_comp_tab. CLEAR l_comp.

  l_comp-name = 'FIELD_DATE'.

  l_comp-type = cl_abap_elemdescr=>get_d( ).

  APPEND l_comp TO l_comp_tab. CLEAR l_comp.

  IF l_comp_tab[] IS NOT INITIAL.

    CONCATENATE 'TAB_' lv_tabix INTO lv_tab_name.

    CONDENSE lv_tab_name.

    CLEAR: l_estrutura, lo_new_tab.

    TRY .

        l_estrutura = cl_abap_structdescr=>create( l_comp_tab ).

        TRY.

            lo_new_tab = cl_abap_tabledescr=>create( p_line_type  = l_estrutura

                                                     p_table_kind = cl_abap_tabledescr=>tablekind_std

                                                     p_unique     = abap_false ).

            CLEAR l_comp2.

            l_comp2-name = lv_tab_name.

            l_comp2-type = lo_new_tab.

            APPEND l_comp2 TO l_comp_tab2.

          CATCH cx_sy_table_attributes.

            " Error message...

          CATCH cx_sy_table_key_specification.

            " Error message...

        ENDTRY.

      CATCH cx_sy_struct_attributes.

        " Error message...

      CATCH cx_sy_struct_comp_name.

        " Error message...

      CATCH cx_sy_struct_comp_type.

        " Error message...

      CATCH cx_sy_struct_suffix_name.

        " Error message...

    ENDTRY.

  ENDIF.

ENDDO.

TRY.

    CLEAR: l_estrutura.

    l_estrutura = cl_abap_structdescr=>create( l_comp_tab2 ).

    CREATE DATA l_struc_final TYPE HANDLE l_estrutura.

  CATCH cx_sy_struct_comp_name.

    " Error message...

  CATCH cx_sy_struct_creation.

    " Error message...

  CATCH cx_sy_table_creation .

    " Error message...

ENDTRY.

BREAK-POINT. "here, on this breakpoint ...you can analyze the final structure (l_struc_final) created.

5 Comments