Create a dynamic table in SAP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.