Skip to Content
Technical Articles
Author's profile photo Dogu Ozan Kumru

Creating Dynamic Table and Dynamic Select

Hello, in this blog, I will talk about the necessary methods and definitions for the use of dynamic tables in a program, dynamic select and how to perform operations on dynamic table. I hope it will be an example for you to create a dynamic table. Before start, dynamic internal table is an internal table with variable rows and columns which can be defined during run time.

 

Definitions

 

For dynamic table, we create Field-symbol. Remember that “Field symbols can be declared in any procedure and in the global declaration part of an ABAP program, but not in the declaration part of a class or an interface.”
FIELD-SYMBOLS : <table> TYPE STANDARD TABLE.

 

We can see the types, variables and methods that we will use in Class structure below.

CLASS lcl_class DEFINITION.

  PUBLIC SECTION.

  PRIVATE SECTION.

    TYPES:BEGIN OF ts_add_column,
            name  TYPE string,
            types TYPE string,
          END OF ts_add_column,
          tt_add_coll TYPE STANDARD TABLE OF ts_add_column WITH EMPTY KEY,
          BEGIN OF ts_dyn_name,
            name TYPE string,
          END OF ts_dyn_name,
          tt_dyn_name TYPE STANDARD TABLE OF ts_dyn_name WITH EMPTY KEY.

    DATA: gt_dyn_names  TYPE tt_dyn_name,
          gv_string     TYPE string.

    METHODS:
      get_data,
      create_dynamic_table,
      get_dynamic_fields RETURNING VALUE(rt_table) TYPE tt_add_coll,
      get_all_fields     IMPORTING VALUE(is_table) TYPE any
                                   VALUE(it_type)  TYPE tt_add_coll
                         RETURNING VALUE(rv_value) TYPE REF TO data,
      set_datadescr      IMPORTING VALUE(iv_types) TYPE string
                         RETURNING VALUE(rv_descr) TYPE REF TO cl_abap_datadescr.

ENDCLASS.

 

Methods

 

We call create_dynamic_table( ) method to create dynamic table. We add the fields that we dynamically retrieved from the table into a string. Thus, the fields we pull from the table in select also become dynamic.

METHOD get_data.

    create_dynamic_table( ).

    LOOP AT gt_dyn_names INTO DATA(ls_name).
      gv_string = |{ gv_string } c~{ ls_name-name },|.
    ENDLOOP.

    gv_string = |{ gv_string } c~poper, c~waers, c~prtyp, data~bwkey, data~matnr, data~mtart, data~matkl, data~bklas, data~bwtar, data~prctr, data~kalnr,|.
    gv_string = |{ gv_string } @p_bdatj AS bdatj, @p_bukrs AS bukrs, CASE WHEN c~prtyp = 'V' THEN 'Fiili' WHEN c~prtyp = 'S' THEN 'Plan' ELSE ' ' END AS stat|.

    SELECT ( gv_string ) 
      FROM ckmlprkeph AS c
     INNER JOIN ckmlhd ON ckmlhd~kalnr = c~kalnr
     INNER JOIN @lt_data AS data                   “itab join
                       ON data~kalnr = c~kalnr
     WHERE c~bdatj EQ @p_bdatj
       AND c~poper IN @s_poper
       AND c~curtp EQ @p_curtp
       AND c~kkzst EQ ''
      INTO CORRESPONDING FIELDS OF TABLE @<table>.

 

We use field-symbol and assign component for the operations to be performed on dynamic table. We can also give a key to sort dynamically using the method in the example. For now we will use a fixed key. We can use the following examples.
LOOP AT <table> ASSIGNING FIELD-SYMBOL(<fs_table>).
      ASSIGN COMPONENT 'PRTYP' OF STRUCTURE <fs_table> TO FIELD-SYMBOL(<fs_prtyp>).
      IF <fs_prtyp> EQ 'V'.
        ASSIGN COMPONENT 'KALNR' OF STRUCTURE <fs_table> TO FIELD-SYMBOL(<fs_kalnr>).
        READ TABLE <table> ASSIGNING FIELD-SYMBOL(<fs_v>) WITH KEY ('PRTYP') ='V' ('KALNR') = <fs_kalnr>.
        READ TABLE <table> ASSIGNING FIELD-SYMBOL(<fs_s>) WITH KEY ('PRTYP') = 'S' ('KALNR') = <fs_kalnr>.
        APPEND INITIAL LINE TO <table> ASSIGNING FIELD-SYMBOL(<fs_line>).
        MOVE-CORRESPONDING <fs_v> TO <fs_line>.
        ASSIGN COMPONENT 'PRTYP' OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_l_prtyp>).
        <fs_l_prtyp> = 'X'.
        ASSIGN COMPONENT 'STAT' OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_durum>).
        <fs_durum> = 'Fark'.
        LOOP AT gt_dyn_names INTO DATA(ls_dyn_names).
          ASSIGN COMPONENT ls_dyn_names-name OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_l_kst>).
          ASSIGN COMPONENT ls_dyn_names-name OF STRUCTURE <fs_s> TO FIELD-SYMBOL(<fs_s_kst>).
          <fs_l_kst> -= <fs_s_kst>.
        ENDLOOP.
      ENDIF.
    ENDLOOP.

    UNASSIGN <fs_table>.


    LOOP AT <table> ASSIGNING <fs_table>.
      LOOP AT gt_dyn_names INTO ls_dyn_names.
        ASSIGN COMPONENT ls_dyn_names-name OF STRUCTURE <fs_table> TO FIELD-SYMBOL(<fs_kst>).
        ASSIGN COMPONENT 'TOTAL' OF STRUCTURE <fs_table> TO FIELD-SYMBOL(<fs_total>).
        <fs_total> += <fs_kst>.
      ENDLOOP.
    ENDLOOP.

    UNASSIGN <fs_table>.

    SORT <table> ASCENDING BY ('KALNR'). 
  ENDMETHOD.

 

After we give the fixed areas that we will use into the structure we have created, we give this structure to function. We call get_dynamic_fields( ) for dynamic fields as well. Then we give the data reference we obtained to table.

The purpose of using the ZSTATIC_ST static structure is to add the predefined fields to the table. We can use it with dynamic fields to be determined during the runtime. Required fields can be included in table fields by adding them to this structure. Or not use it at all.

METHOD create_dynamic_table.

    DATA(dref) =  get_all_fields(  is_table =  'ZSTATIC_ST'  it_type = get_dynamic_fields( )  ).
    ASSIGN dref->* TO <table>.

  ENDMETHOD.

 

We decide which fields we want to use with the data from the selection. We assign the field names to the table to be used in the operations and dynamic select. We assign the field names and types for dynamic type into table.

METHOD get_dynamic_fields.

    SELECT concat( 'KST', tckh3~el_hv ) AS el_hv,
           tckh1~txele
      FROM tckh3
     INNER JOIN tckh1 ON tckh3~elehk = tckh1~elehk
                     AND tckh3~elemt = tckh1~elemt
     WHERE tckh3~elehk EQ 'Z1'
      INTO TABLE @DATA(lt_tckh).

    gt_dyn_names = VALUE tt_dyn_name( FOR ls_tckh IN lt_tckh ( name = ls_tckh-el_hv ) ).

    IF sy-subrc = 0.
      rt_table = VALUE #( FOR ls_tckh IN lt_tckh ( name = ls_tckh-el_hv types = 'MLCCS_D_KSTEL' ) ).
    ENDIF.

  ENDMETHOD.

 

We enclose fields from structure in lt_comp. We import dynamic fields from the table containing it into lt_comp. After creating table containing all fields, we return the table type with rv_value.

METHOD get_all_fields.

    DATA : ref_table_des TYPE REF TO cl_abap_structdescr,
           lt_comp       TYPE cl_abap_structdescr=>component_table.

    ref_table_des ?= cl_abap_typedescr=>describe_by_name( is_table ).
    lt_comp  = ref_table_des->get_components( ). 

    APPEND LINES OF VALUE cl_abap_structdescr=>component_table( FOR is_type IN it_type ( name = is_type-name type = set_datadescr( is_type-types ) ) ) TO lt_comp.

    DATA(lo_new_type) = cl_abap_structdescr=>create( lt_comp ).
    DATA(lo_new_tab) = cl_abap_tabledescr=>create(
                        p_line_type  = lo_new_type
                        p_table_kind = cl_abap_tabledescr=>tablekind_std
                        p_unique     = abap_false ).

    CREATE DATA rv_value TYPE HANDLE lo_new_tab.

  ENDMETHOD.

  METHOD set_datadescr.
    rv_descr ?= cl_abap_elemdescr=>describe_by_name( iv_types ).
  ENDMETHOD.

 

Summary

 

To summarize, we create table with both dynamic and static fields. After this dynamic process, we made a dynamic selection while filling this table. We use the terms field-symbols and assign component to make changes on dynamic table. And so we have obtained a completely dynamic process.

 

Conclusion

 

With this blog you can create your own dynamic table and selections. You can write your thoughts and questions in the comment section. If you think this blog is helpful please do not forget to like and share 🙂 .

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Sorry, I don't get it...

      1. It starts with "short program about dynamic table..." but what for? What's the purpose of this program? What does it do? How is it "dynamic" exactly?
      2. To be honest, I felt like not reading beyond "create Field-symbol in TOP include" (I've never created a single TOP include in my life and if someone needs a new one in 2022, I'd say something is off with the design there). But OK, thought I'd give it a benefit of doubt.
      3. It jumps right into showing the class definition, again, without explaining any thought behind it. OK, maybe it'll become more clear later.
      4. "We call create_dynamic_table( ) function to create dynamic type" - more confusion. It's not a function, it's a method. It does not create a "dynamic type" (I'm honestly not sure what that would be) and if it did, why would the method be called "...table"? The code fragment that follows presents... method get_data. Where did that come from and how is this all supposed to come together?
      5. OK, I find create_dynamic_table( ) code somewhere down the line. But it, in turn, calls the method get_all fields that is shown even further down. Then there is also get_dynamic_fields( ) that is somehow involved and lets me to believe there are... also static fields? And it appears that ZSTATIC_ST table is used but the definition is not shared, so it's anyone's guess.

      This is where I decided to fold it. Maybe it's just me but I suspect you might want to explain better what you're trying to do here and what is this meant to solve or improve.

      Thank you.

      Author's profile photo Dogu Ozan Kumru
      Dogu Ozan Kumru
      Blog Post Author

      Thanks for your feedback. I will make the necessary corrections.

      1. Dynamic internal table is an internal table with variable rows and columns which can be defined during run time. If I add a definition like this, I think problem can be solved.
      2.  "Field symbols can be declared in any procedure and in the global declaration part of an ABAP program, but not in the declaration part of a class or an interface." I recommended the TOP included in order to be understandable by beginners. I wanted to explain why it is not defined in Class definition. We don't have to use TOP include, I'll add that too.
      3.  You're right, the code I've shown is just the part of the program related to the dynamic table part, I'll add it to the description. I will also update it as a method.
      4. The purpose of using a static structure is to add predetermined fields and dynamic fields to be determined during run time. This code is actually an example. You can use these codes for your own purpose. So it doesn't matter what's in ZSTATIC_ST. Required fields can be included in table fields by adding them to this structure. Or may not use it at all.

      Hopefully, it has become more understandable after the corrections. Thanks.