Dynamic Internal Table Creation using RTTS

The use of CL_ALV_TABLE_CREATE class for creating dynamic tables is limited to the amount of 36 internal tables.

You can handle the error does not cause the "dump", but will not meet the need for an unrestricted number of tables.

The best solution to this is the use of RTTS.

Dynamic Internal Table Creation using RTTS

Shows how to use the RTTS to create a dynamic internal table. Lets Checkout how we can create internal table at run time using RTTS.

From the ABAP release 6.40, SAP has provided RTTS – Run Time Type Services to create types, internal tables at run-time. This RTTS can also be used to describe the properties of the types as well as the fields, internal tables etc.

Sometimes, when we write a program, we don’t have all the information of the fields of the internal table. For example: we are accessing the cost element Actul posting data from the table COSP. Now, we have a requirement to generate an output which will have some specified columns – like amount of period 4 to 8 or Amount of period 1 to 3 or some other combination. This kind of secnarioes are perfect examples of the RTTS. We will see how to create a dynamic internal table using this example.

Steps to Create Dynamic ITAB

To create a dynamic internal table, we need to:
1. Gather all the Components
2. Generate a Type from this components
3. Generate a Table Type from this created type
4. Create a Data reference of this Table Type
5. Assign this data reference to the Field-Symbol of table type. This Field-symbol will act as our dyanmic internal table

Demo Application

In today’s example we will see how to:
1. Create Dynamic internal table
2. Write dynamic select query to data into this dynamic table
3. Change the contents of this dynamic internal table
4. Generate the ALV display using SALV model for this dynamic internal table. Explore how to create ALV using SALV from Tutorials > SALV Table Display

We will provide the selection of the period for which user wants to generate an output. Based on the entered periods we will create a dynamic type containg the KSTAR (Costing Element) and Amount fields for the month. After creating the dynamic type, we will create a dynamic table type. Using this table type we will create a reference of the data. From this data reference we will assign the internal table to field-symbols.

So, let’s see the code snippet to create Dynamic ITAB:

*&---------------------------------------------------------------------*
*& This Code snippet shows how to
*&   Create Dynamic Internal Table
*&   Dynamic Selection of data
*&   Accessing Dynamic data selection
*&   Displaying Dynamic internal table in ALV
*&---------------------------------------------------------------------*
report zdynamic_itab.
*
* Exisiting Table type
TYPES: BEGIN OF ty_kstar,
       kstar TYPE kstar,
       END   OF ty_kstar.
*
* Dynamic Table creation
DATA: lo_struct   TYPE REF TO cl_abap_structdescr,
      lo_element  TYPE REF TO cl_abap_elemdescr,
      lo_new_type TYPE REF TO cl_abap_structdescr,
      lo_new_tab  TYPE REF TO cl_abap_tabledescr,
      lo_data     TYPE REF TO data,
      lt_comp     TYPE cl_abap_structdescr=>component_table,
      lt_tot_comp TYPE cl_abap_structdescr=>component_table,
      la_comp     LIKE LINE OF lt_comp,
      lf_months   TYPE monat,
      lf_run_mon  TYPE monat.
*
* Dynamic Selection fields
TYPES: BEGIN 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
FIELD-SYMBOLS: <f_tab>   TYPE ANY TABLE,
               <f_line>  TYPE ANY,
               <f_field> TYPE ANY.
*
* Selection Screen
PARAMETERS: p_mon_fr TYPE monat,
            p_mon_to TYPE monat.
*
START-OF-SELECTION.
*
*$*$*...............Dynamic Internal Table........................*$*$*
* 1. Getting Compoents from existing type
  lo_struct ?= cl_abap_typedescr=>describe_by_name( 'TY_KSTAR' ).
  lt_comp  = lo_struct->get_components( ).
  APPEND LINES OF lt_comp TO lt_tot_comp.
*
* 2. Adding required fields based on the single data element
* Determining Number of fields
  lf_months = ( p_mon_to - p_mon_fr ) + 1.
  lf_run_mon = p_mon_fr.
*
  DO lf_months TIMES.
*
*   Element Description
    lo_element ?= cl_abap_elemdescr=>describe_by_name( 'WTGXXX' ).
*
*   Field name
    CONCATENATE 'WTG0' lf_run_mon INTO la_comp-name.
*
*   Field type
    la_comp-type = cl_abap_elemdescr=>get_p(
                      p_length   = lo_element->length
                      p_decimals = lo_element->decimals ).
*
*   Filling the component table
    APPEND la_comp TO lt_tot_comp.
    CLEAR: la_comp.
*
    lf_run_mon = lf_run_mon + 1.
  ENDDO.
*
* 3. Create a New Type
  lo_new_type = cl_abap_structdescr=>create( lt_tot_comp ).
*
* 4. New Table type
  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 ).
*
* 5. data to handle the new table type
  CREATE DATA lo_data TYPE HANDLE lo_new_tab.
*
* 6. New internal table in the fieldsymbols
  ASSIGN lo_data->* TO <f_tab>.
*
*$*$*...............Dynamic Selection.............................*$*$*
* Filling up the table for the Selection fields of Select Query
  LOOP AT lt_tot_comp INTO la_comp.
    la_fields-field = la_comp-name.
    APPEND la_fields TO lt_fields.
    CLEAR: la_comp, la_fields.
  ENDLOOP.
*
* 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'.
  ENDLOOP.
*
*
*$*$*...............Displaying using SALV model...................*$*$*
*
  DATA: lo_alv TYPE REF TO cl_salv_table.
*
  TRY.
      cl_salv_table=>factory(
        EXPORTING
          list_display = abap_false
        IMPORTING
          r_salv_table = lo_alv
        CHANGING
          t_table      = <f_tab> ).
    CATCH cx_salv_msg .
  ENDTRY.
*
  lo_alv->display( ).

 

Check out all ways to Create Dynamic ITAB

You would love to check out all other ways to create Dynamic Internal Tables.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChampaignWolf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值