*&---------------------------------------------------------------------* *& 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( ).
Copied from here