You are on page 1of 2

*&-------------------------------------------------------------------*& Report ZCREATE_DYNAMIC_TABLE

*& Description: How to Create a Dynamic Table


*&-------------------------------------------------------------------REPORT zcreate_dynamic_table.
*&-------------------------------------------------------------------*&STEP: ONE
*&-------------------------------------------------------------------*& This struct will content all fields of our dynamic table
DATA: lt_comp
TYPE cl_abap_structdescr=>component_table.
*& 'str_type' will have new dynamic structure created
DATA: str_type TYPE REF TO cl_abap_structdescr.
*& 'tab_type' will have new dynamic table created
DATA: tab_type TYPE REF TO cl_abap_tabledescr.
*& 'lt_data' will be our point to reference data
DATA: lt_data
TYPE REF TO data.
*& work area to handle attributes and name of each fields
DATA: la_comp
LIKE LINE OF lt_comp.
*& variable to build name of each field
DATA: field_name
TYPE txt30.
DATA: field_number TYPE text10.
*& Field Symbol to handle Table > Final Outcome
FIELD-SYMBOLS: <t_tab> TYPE STANDARD TABLE.
START-OF-SELECTION.
*&-------------------------------------------------------------------*& STEP: TWO
*&-------------------------------------------------------------------*& At this Point we will Populate Internal Table table lt_comp with
*& three fields called: FIELD01, FIELD02, FIELD03
*&-------------------------------------------------------------------DO 3 TIMES.
CLEAR: field_name.
WRITE sy-index TO field_number.
CONDENSE field_number.
CONCATENATE 'FIELD0' field_number INTO field_name.
*& Name for Each Field
la_comp-name = field_name.
*&

Now will build all three with type C and lenght 1024
la_comp-type = cl_abap_elemdescr=>get_c( p_length = 1024 ).
APPEND la_comp TO lt_comp.
CLEAR: la_comp.
ENDDO.
*&-------------------------------------------------------------------*& STEP. Three
*& Now we must create a Structure with all fields loaded before.
*&-------------------------------------------------------------------str_type = cl_abap_structdescr=>create( lt_comp ).
*& In this step we create a type table by using the structure
STR_TYPE,

*& and passing 2 Additional Parameters: p_table_kind (Standard,


Hashed, Sorted)
*& p_unique (this to indicate if have unique key or not)
tab_type = cl_abap_tabledescr=>create( p_line_type = str_type
p_table_kind =
cl_abap_tabledescr=>tablekind_std
p_unique
= abap_false ).
*&-------------------------------------------------------------------*& STEP: FOUR
*&-------------------------------------------------------------------*& Data to handle the new table type. We could catch an exception
here.
TRY.
CREATE DATA lt_data TYPE HANDLE tab_type.
CATCH cx_sy_create_data_error.
ENDTRY.
*&-------------------------------------------------------------------*& STEP: FIVE
*&-------------------------------------------------------------------*& New internal table referenced by field symbol
TRY.
ASSIGN lt_data->* TO <t_tab>.
CATCH cx_sy_assign_cast_illegal_cast.
CATCH cx_sy_assign_cast_unknown_type.
CATCH cx_sy_assign_out_of_range.
ENDTRY.
*&

at this point you will have a reference to your dynamic table type
BREAK-POINT.

You might also like