Dynamic Internal Table Creation

Posted by Krishh Webworld | 12:35 PM | , , | 1 comments »

Shows how to use the RTTS to create a dynamic internal table


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.

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

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

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:
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: TYPE ANY TABLE,
TYPE ANY,
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 .
*
*$*$*...............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
FROM cosp
UP TO 10 ROWS.
*
*$*$*...............Accessing dynamic table.......................*$*$*
LOOP AT ASSIGNING .
ASSIGN COMPONENT 'WTG004' OF STRUCTURE TO .
= '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 = ).
CATCH cx_salv_msg .
ENDTRY.
*
lo_alv->display( ).

1 comments

  1. ftomi76 // March 26, 2012 at 2:53 AM  

    field symbols are not displayed correctly in the source code. Could you please correct it, or suggest another way to get the full-correct source code ?

Archives

Subscribe Now: Feed Icon