SAP ABAP Example for Creating dynamic internal table

In SAP sometimes we need to create internal table at runtime, in this case we can make use of the method create_dynamic_table from the class cl_alv_table_create.

Steps:

1. Create the field catalogue for the internal table.

2. Call the method  create_dynamic_table from the class cl_alv_table_create.

Example:

Data: wa_fieldcat TYPE lvc_s_fcat,
it_fieldcat TYPE lvc_t_fcat.

  DATAt_newtable TYPE REF TO data,
wa_newline  TYPE REF TO data.

FIELD-SYMBOLS<t_dyntable> TYPE STANDARD TABLE,  ” Dynamic internal table name
<fs_dyntable>.  “Field symbol to create work area

*****Build feild catalogue

     CLEAR wa_fieldcat.
wa_fieldcat-fieldname ‘PERNR’.
wa_fieldcat-datatype  ‘CHAR’.
wa_fieldcat-intlen    8.
APPEND wa_fieldcat TO it_fieldcat.

   CLEAR wa_fieldcat.
wa_fieldcat-fieldname ‘BUKRS’.
wa_fieldcat-datatype  ‘CHAR’.
wa_fieldcat-intlen    4.
APPEND wa_fieldcat TO it_fieldcat.

*add more fields like this depending on your requirement.

CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog it_fieldcat
IMPORTING
ep_table        t_newtable.

ASSIGN t_newtable->TO <t_dyntable>. “Now you can use <t_dyntable> as you internal table

***work area creation

CREATE DATA wa_newline LIKE LINE OF <t_dyntable>.
ASSIGN wa_newline->TO <fs_dyntable>.

Now field symbol <t_dyntable> will act as an internal table of your structure.

 

 

0 Comments

Leave A Reply