Skip to Content
Author's profile photo Former Member

Create dynamic table into dynamic structure

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

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Joao Sousa
      Joao Sousa

      Maybe it would be better if you add a bit of text in the beginning to explain what this program will actually do, instead of putting the code directly.

      Author's profile photo Florian Henninger
      Florian Henninger

      Yep,

      and maybe this isn't a real blog entry, moreover it is a wiki-entry, perhaps a document, perhaps

      See:

      • Blog Post: Tell a story or share an experience that benefits community members.
      • Document: Help other members in their work with articles and "how-to" guides.

      Create a Blog Post

      Easy to find in the right upper corner called Getting Starded. It's worse to read through 😉

      ~Florian

      PS: But nice showcase anyway.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      sorry, but I felt a lot of lack of example of these ... just wanted to help.

      Author's profile photo Florian Henninger
      Florian Henninger

      Hi Adriano,

      as I said. It is a useful snippet. I just wanted to show you the difference for future posts.

      So don't feel sorry about it.

      ~Florian