Create/ Access Dynamic Internal Table - SAP ABAP


Dynamic Internal Table is the extension of the internal table concept which is used in the program to store itemized data with the fact that the number of columns are not known at the type of declaring the structure of the table. The final structure of the table is known at the runtime or compile time.


How To Create Dynamic Internal Table

There are few ways to declare the dynamic internal table. Now based on the requirement we need to decide which way we need to proceed for table creation.

1st Way:


Here from selection screen (p_table) we are getting the table name whose data needs to be displayed.


Now in the table <t_itab> we are having 20 records from MARA table stored to be displayed.


2nd Way: 

Now the second way we can create the dynamic internal table is to using dynamic field catalog based on which the dynamic table will be created.


Here we are dynamically populating the lt_fcat. Here few fields are fixed like aufnr, matnr, charg, werks & apart from that we have few fields to be populated which will be created run time based on w_level which is being populated based on the entries returned by a standard FM.



Now using the standard class method cl_alv_table_create=>create_dynamic_table we are generating the table reference of the dynamic table & using the table reference we are creating the dynamic table <ft_shadow>.



How To Populate Dynamic Internal Table:

Now once we have created the dynamic internal table we need to populate with the data.


Here we are using an internal table ct_shadow to populate the dynamic table <ft_shadow>. Here we are appending the initial blank line to the dynamic table. After that we are assigning the fixed fields of the dynamic internal table like matnr, aufnr, werks, charg using 

ASSIGN COMPONENT c_matnr OF STRUCTURE <lfs_shadow> TO <lfw_wa>.

Here <lfs_shadow> is pointing to the initial line appended to the structure <ft_shadow> using

APPEND INITIAL LINE TO <ft_shadow> ASSIGNING <lfs_shadow>. 

Similar way we can populate the dynamic fields of the dynamic internal table.



In this case the dynamic field name that will be generated in run time are named as COL1, COL2, COL3 ….COLN.

So using the ASSIGN COMPONENT lw_col OF STRUCTURE <lfs_shadow> TO <lfw_wa> we are populating the dynamic fields.

Here lw_col is being populated by concatenating ‘COL’ & the number which is generated dynamically like 1, 2 , 3.. N.


How To Access The Data Of The Dynamic Internal Table:

Now to access the data of the dynamic internal table is almost similar as to populating the data with one restriction. We can’t read the dynamic table entries based on a certain condition. We can read using index or if we need to access the data based on certain condition we need to loop it & then check it.


Here we need to compare aufnr field. So we looped it & then accessing the aufner field for each record. Then we are comparing the value of the field with <lfs_shadow_c>-aufnr.