Today let’s learn how to create a dynamic internal table using ABAP, in this tutorial I’ll use CREATE_DYNAMIC_TABLE method to generate the internal table.
REPORT Z_CREATE_DYNAMIC_INTERNAL_TABLE. "A sample Code to generate Dynamic Internal table "http://www.freesaptutorial.com DATA: TY_DTRF TYPE REF TO DATA, ALVF_CAT TYPE TABLE OF LVC_S_FCAT, LS_ALV_CAT LIKE LINE OF ALVF_CAT. TYPES TABNAME LIKE DCOBJDEF-NAME . PARAMETER: P_TBNAME TYPE TABNAME. DATA: BEGIN OF ITAB OCCURS 0. INCLUDE STRUCTURE DNTAB. DATA: END OF ITAB. DATA: new_line TYPE REF TO data. FIELD-SYMBOLS: <fs_data> TYPE REF TO data, <fs_1> TYPE ANY TABLE, <fs_2>, <fs_3>. FIELD-SYMBOLS : <F_FS> TYPE TABLE, <F_FS1> TYPE TABLE, <F_FS2> TYPE ANY, <F_FS3>. REFRESH ITAB. "Using NAMETAB_GET to get the tablename structure CALL FUNCTION 'NAMETAB_GET' EXPORTING LANGU = SY-LANGU TABNAME = P_TBNAME TABLES NAMETAB = ITAB EXCEPTIONS NO_TEXTS_FOUND = 1. LOOP AT ITAB . LS_ALV_CAT-FIELDNAME = ITAB-FIELDNAME. LS_ALV_CAT-REF_TABLE = P_TBNAME. LS_ALV_CAT-REF_FIELD = ITAB-FIELDNAME. APPEND LS_ALV_CAT TO ALVF_CAT. ENDLOOP. *Using function CREATE_DYNAMIC_TABLE to create the internal table CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE EXPORTING IT_FIELDCATALOG = ALVF_CAT IMPORTING EP_TABLE = TY_DTRF . ASSIGN TY_DTRF->* TO <F_FS>. "Select for the transparent table into the internal table SELECT * FROM (P_TBNAME) INTO CORRESPONDING FIELDS OF TABLE <F_FS>. "Now here's how we get the value of the dynamic internal table "By using Assign Component [column index] of STRUCTURE "The first dynamic internal table is COMPONENT 1 LOOP AT <F_FS> ASSIGNING <F_FS2>. "COMPONENT 1 is the first column in Dynamic Internal table ASSIGN COMPONENT 1 OF STRUCTURE <F_FS2> TO <F_FS3>. WRITE: / <F_FS3>. ASSIGN COMPONENT 2 OF STRUCTURE <F_FS2> TO <F_FS3>. WRITE: <F_FS3>. ASSIGN COMPONENT 3 OF STRUCTURE <F_FS2> TO <F_FS3>. WRITE: <F_FS3>. ASSIGN COMPONENT 4 OF STRUCTURE <F_FS2> TO <F_FS3>. WRITE: <F_FS3>. ASSIGN COMPONENT 5 OF STRUCTURE <F_FS2> TO <F_FS3>. WRITE: <F_FS3>. ENDLOOP.
Save and activate the code.
Type in any transparent table name and click execute.

Here’s the Dynamic internal table result.

Popularity: 12% [?]