REPORT zg_dynamic_ex LINE-SIZE 300.
TYPE-POOLS: abap.
FIELD-SYMBOLS: <dyn_table> type standard table,
<Dyn_wa>,
<Dyn_field>.
DATA: dy_table type ref to data,
Dy_line type ref to data,
Xfc TYPE lvc_s_fcat,
Ifc TYPE lvc_t_fcat.
SELECTION-screen begin of block b1 with frame.
PARAMETERS: p_table (30) TYPE c DEFAULT 't001 '.
SELECTION-screen end of block b1.
START-OF-SELECTION.
PERFORM get_structure.
PERFORM create_dynamic_itab.
PERFORM get_data.
PERFORM write_out.
*&--------------------------------------------------------------------*
* & Form get_structure
*&--------------------------------------------------------------------*
* Text
*---------------------------------------------------------------------*
FORM get_structure.
DATA: idetails TYPE abap_compdescr_tab,
Xdetails TYPE abap_compdescr.
DATA: ref_table_des type ref to cl_abap_structdescr.
Ref_table_des? =
Cl_abap_typedescr => describe_by_name (p_table ).
Idetails [] = ref_table_des-> components [].
Loop at idetails INTO xdetails.
CLEAR xfc.
Xfc-fieldname = xdetails-name.
The xfc-datatype = xdetails-type_kind.
The xfc-inttype = xdetails-type_kind.
Xfc-intlen = xdetails-length.
Xfc-decimals = xdetails-decimals.
APPEND xfc TO ifc.
ENDLOOP.
ENDFORM. "get_structure
*&--------------------------------------------------------------------*
* & Form create_dynamic_itab
*&--------------------------------------------------------------------*
* Text
*---------------------------------------------------------------------*
FORM create_dynamic_itab.
Call method cl_alv_table_create => create_dynamic_table
EXPORTING
It_fieldcatalog = ifc
IMPORTING
Ep_table = dy_table.
ASSIGN dy_table-> * TO <dyn_table>.
Create data dy_line like line of <dyn_table>.
ASSIGN dy_line-> * TO <dyn_wa>.
ENDFORM. "create_dynamic_itab
*&--------------------------------------------------------------------*
* & Form get_data
*&--------------------------------------------------------------------*
* Text
*---------------------------------------------------------------------*
FORM get_data.
SELECT * into table <dyn_table> FROM (p_table ).
ENDFORM. "get_data
*&--------------------------------------------------------------------*
* & Form write_out
*&--------------------------------------------------------------------*
* Text
*---------------------------------------------------------------------*
FORM write_out.
Loop at <dyn_table> INTO <dyn_wa>.
DO.
Assign component sy-index of structure <dyn_wa> TO <dyn_field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
IF sy-index = 1.
WRITE:/<dyn_field>.
ELSE.
WRITE: <dyn_field>.
ENDIF.
ENDDO.
ENDLOOP.
ENDFORM. "write_out