Dynamic Internal Table Creation using class CL_ALV_TABLE_CREATE

By | November 19, 2008 | Dynamic ITAB | 20,663 | 5

Describes how to show the Dynamic Internal Table Creation using class CL_ALV_TABLE_CREATE

In the post Dynamic Internal Table Creation and Dynamic Internal Table with Deep structure, we have seen how we can use the RTTS – Run Time Type Services to create dynamic internal table.

Today we will eloborate another way of creating the dynamic internal table. We will see how we can use the class CL_ALV_TABLE_CREATE to create a dynamic internal table for ALV.

Basic pricipal would be, we need to fill the field catalog table and pass it to static method CREATE_DYNAMIC_TABLE from class CL_ALV_TABLE_CREATE.

We will use the same example as the reference in the post Dynamic Internal Table Creation.

Here is the code snippet:

*& Report  ZTEST_NP_DYN
*& This Code snippet shows how to
*&   Create Dynamic Internal Table using the fieldcatalog and 
*&     class CL_ALV_TABLE_CREATE
*&   Dynamic Selection of data
*&   Accessing Dynamic data selection
*&   Displaying Dynamic internal table in ALV
REPORT  ztest_np_dyn.
DATAlt_fieldcat TYPE lvc_t_fcat,
      la_fieldcat TYPE lvc_s_fcat,
      lo_table    TYPE REF TO data.
DATAlf_months  TYPE monat,
      lf_run_mon TYPE monat.
* Dynamic Selection fields
TYPESBEGIN OF ty_fields,
       field TYPE char30,
       END   OF ty_fields.
DATA:  lt_fields TYPE STANDARD TABLE OF ty_fields,
       la_fields TYPE ty_fields.
* field symbols to access the dynamic table
               <f_line>  TYPE ANY,
               <f_field> TYPE ANY.
* Selection Screen
PARAMETERSp_mon_fr TYPE monat,
            p_mon_to TYPE monat.
* 1. Adding fields in the field catalog
  la_fieldcat-fieldname 'KSTAR'.
  la_fieldcat-datatype  'CHAR'.
  la_fieldcat-outputlen 10.
  APPEND la_fieldcat TO lt_fieldcat.
  CLEAR  la_fieldcat.
* 2. Adding required fields based on the selection months
* Determining Number of fields
  lf_months p_mon_to p_mon_fr 1.
  lf_run_mon p_mon_fr.
  DO lf_months TIMES.
*   Field name
    CONCATENATE 'WTG0' lf_run_mon INTO la_fieldcat-fieldname.
    la_fieldcat-datatype  'CURR'.
    la_fieldcat-outputlen 17.
*   Filling the fieldcatalog
    APPEND la_fieldcat TO lt_fieldcat.
    CLEAR  la_fieldcat.
    lf_run_mon lf_run_mon + 1.
* Calling method to generate the dynamic internal table based on
* ALV field catalog
      it_fieldcatalog lt_fieldcat
      ep_table lo_table ).
  ASSIGN lo_table->TO <f_tab>.
*$*$*...............Dynamic Selection.............................*$*$*
* Filling up the table for the Selection fields of Select Query
  LOOP AT lt_fieldcat INTO la_fieldcat.
    la_fields-field la_fieldcat-fieldname.
    APPEND la_fields TO lt_fields.
* Selecting data
  SELECT (lt_fields)
         INTO  TABLE <f_tab>
         FROM  cosp
         UP TO 10 ROWS.
*$*$*...............Accessing dynamic table.......................*$*$*
  LOOP AT <f_tab> ASSIGNING <f_line>.
    ASSIGN COMPONENT 'WTG004' OF STRUCTURE <f_line> TO <f_field>.
    <f_field> '100.00'.
*$*$*...............Displaying using SALV model...................*$*$*
  DATAlo_alv TYPE REF TO cl_salv_table.
          list_display abap_false
          r_salv_table lo_alv
          t_table      <f_tab> ).
    CATCH cx_salv_msg .

I would suggest to use the RTTS to create dynamic internal table as depicted in posts:

  • Dynamic Internal Table Creation
  • Dynamic Internal Table with Deep structure
  • Like It? Share!!

    Don't miss an Update

    Get notified of the new post, right into your inbox

    Naimesh Patel{273 articles}

    I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
    Follow :

    Explore all of his 273 articles.

    Load comments


    • Anonymous


      i have a requirement..where i have data in dynamic internal table .i need to display it in the function module(may be copying in static internal table)


    • Hi Sam,

      I am not following your requirement completely but, if you want to generate the Table outside the FM and access in within the FM, you can use either TYPE REF TO DATA or declare the table parameter without any type casting.

      Naimesh Patel

    • Jayapranikash N


      (Internal Purpose)
      I have requirement like, In selection screen i have passed parameter value in the name of Standard Table dynamically. For example in parameter i have passed EKKO, EKPO,EKET,EKES…at one by one….In that select query i given up-to 20 records.I got records an Internal table.

      Now i want to display the records…

      if you know kindly suggest me….

      Advance Thanks,


    • Ricardo Pires

      First of all, thank you Naimesh Patel for this wonderful tutorial on creating dynamic internal table using the class CL_ALV_TABLE_CREATE. I followed up your tutorial flawlessly and i was able to create a dynamic internal table and its respective ALV using the aforementioned class. However, the ALV does not show me the header of the table (the line with the names of the columns of the table). I filled my Field Catalog as follows:

        DATA: it_cat TYPE LVC_T_FCAT,
              wa_cat TYPE LVC_S_FCAT.
      clear wa_cat.
        wa_cat-col_pos   = 2.
        wa_cat-fieldname = 'TYPE'.
        wa_cat-datatype  = 'CHAR'.
        wa_cat-inttype   = 'C'.
        wa_cat-intlen    = 10.
        wa_cat-seltext   = 'Character Field Length '.
        append wa_cat to it_cat.

      I think that there is not anything wrong with that but i may be mistaken. I have also checked using the R_COLUMS’s method SET_HEADERS_VISIBLE that the header should have been displayed. Any suggestions on how to get the header to be shown in my ALV?

      Thanks beforehand,
      Ricardo Pires

    • Friedrich


      I have now successfully applied this method to create a dynamic internal table with the fields my user specifies in a dialog. One part of my troubles is solved.
      The other one remaining is: It is crucial that these fields – all names are the technical names of existing DB fields – have the same datatype as in the DB table(s): Specifically, I have a field ERDAT (the field is just an example for any of the type) which, in the DB tables, has datatype D – so when my SELECT pulls it from the DB tables and tries to put it into an internal table, the corresp. field in that internal table needs to be of datatype D, otherwise I’ll be in trouble.
      Is there a way to modify the method so that the fields just automatically assume the attributes (datatype) of the DB fields if I specify a table name to go with it?
      I hope this is understandable: If my user enters the tabname KNA1 in the dialog and the field ERDAT, I can specify those variables and feed them into i_fcat and they will be in the table generated by this method – but I need the field to have the same datatype as KNA1-ERDAT, that is, datatype D.
      Anybody know how to do this?


    Comments on this Post are now closed. If you have something important to share, you can always contact me.

    You seem to be new here. Subscribe to stay connected.