Internal tables are a core concept for storing structured data. You can create them dynamically at runtime using RTTS (Runtime Type Services).
DATA: lo_descr TYPE REF TO cl_abap_structdescr,
lo_table TYPE REF TO cl_abap_tabledescr,
lr_data TYPE REF TO data,
lr_line TYPE REF TO data.
“— Step 1: Define a structure dynamically
lo_descr = cl_abap_structdescr=>create(
VALUE #( ( name = ‘FIELD1’ type = cl_abap_elemdescr=>get_c( 10 ) )
( name = ‘FIELD2’ type = cl_abap_elemdescr=>get_i( ) ) )
).
“— Step 2: Create internal table description
lo_table = cl_abap_tabledescr=>create( lo_descr ).
“— Step 3: Create actual data object (the table)
CREATE DATA lr_data TYPE HANDLE lo_table.
ASSIGN lr_data->* TO FIELD-SYMBOL(<itab>).
“— Step 4: Create a line type and assign
CREATE DATA lr_line TYPE HANDLE lo_descr.
ASSIGN lr_line->* TO FIELD-SYMBOL(<wa>).
“— Step 5: Fill dynamically created table
<wa>-field1 = ‘Hello’.
<wa>-field2 = 123.
APPEND <wa> TO <itab>.
<wa>-field1 = ‘World’.
<wa>-field2 = 456.
APPEND <wa> TO <itab>.
“— Step 6: Loop through the dynamic internal table
LOOP AT <itab> ASSIGNING <wa>.
WRITE: / <wa>-field1, <wa>-field2.
ENDLOOP.
Key Points:
- Use RTTS (Runtime Type Services) →
cl_abap_structdescr
,cl_abap_tabledescr
,cl_abap_elemdescr
. - Create data objects at runtime with
CREATE DATA ... TYPE HANDLE
. - Use field symbols (
<itab>
,<wa>
) to access the data.