Of course! As an ABAP developer, creating internal tables dynamically is a powerful technique, especially when dealing with data sources where the structure isn't known at design time (e.g., dynamic pivots, reading spreadsheets, or flexible database queries).
The modern and recommended approach in ABAP is to use RTTS (Run-Time Type Services). For the specific and very common use case of displaying the data in an ALV grid, there is also a convenient helper class.
Here’s a detailed breakdown of both methods.
Method 1: The Standard RTTS Approach (Recommended & Most Flexible)
RTTS is a set of classes (found in package S_RTTS) that allow you to describe, create, and introspect any data type at runtime.
The process involves these key steps:
- Define the Column Structure: Create a table of components (field name and field type).
- Create a Structure Descriptor: Use the component table to create a description of a single row (a structure).
- Create a Table Descriptor: Use the structure descriptor to create a description of a table.
- Create the Data Object: Use the table descriptor to create the actual internal table in memory.
- Access the Table: Use a field symbol to work with your newly created table.
Complete Code Example:
This report dynamically creates a table with a fixed 'ID' column and a variable number of 'MONTH_XX' columns based on user input.
REPORT z_dynamic_internal_table.
PARAMETERS: p_months TYPE i DEFAULT 3. " How many month columns to create
CLASS lcl_main DEFINITION.
PUBLIC SECTION.
METHODS run.
PRIVATE SECTION.
DATA:
" A data reference. This will point to our dynamic table.
gr_dynamic_table TYPE REF TO data.
" A field symbol to access the dynamic table like a real one.
FIELD-SYMBOLS:
<gt_dynamic_table> TYPE STANDARD TABLE.
METHODS create_dynamic_table.
METHODS fill_dynamic_table.
METHODS display_dynamic_table.
ENDCLASS.
CLASS lcl_main IMPLEMENTATION.
METHOD run.
create_dynamic_table( ).
fill_dynamic_table( ).
display_dynamic_table( ).
ENDMETHOD.
METHOD create_dynamic_table.
" Step 1: Define the Column Structure
DATA: lt_components TYPE abap_component_tab,
ls_component LIKE LINE OF lt_components.
" Get type descriptors for the data types we will use.
DATA(lo_type_id) = cl_abap_elemdescr=>get_n( p_length = 8 ).
DATA(lo_type_month) = cl_abap_elemdescr=>get_p( p_length = 8 p_decimals = 2 ).
" --- Add a fixed column ---
ls_component-name = 'ID'.
ls_component-type = lo_type_id.
APPEND ls_component TO lt_components.
" --- Add dynamic columns based on parameter ---
DO p_months TIMES.
ls_component-name = |MONTH_{ sy-index }|. " e.g., MONTH_1, MONTH_2
ls_component-type = lo_type_month.
APPEND ls_component TO lt_components.
ENDDO.
" Step 2 & 3: Create Structure and Table Descriptors
TRY.
" Create a description of the structure (one row)
DATA(lo_struct_descr) = cl_abap_structdescr=>create( lt_components ).
" Create a description of a table based on that structure
DATA(lo_table_descr) = cl_abap_tabledescr=>create( lo_struct_descr ).
CATCH cx_sy_struct_creation cx_sy_table_creation.
MESSAGE 'Error creating dynamic type description.' TYPE 'E'.
RETURN.
ENDTRY.
" Step 4: Create the Data Object
TRY.
" Create the actual internal table in memory. gr_dynamic_table now holds the address.
CREATE DATA gr_dynamic_table TYPE HANDLE lo_table_descr.
CATCH cx_sy_create_data_error.
MESSAGE 'Error creating dynamic data object.' TYPE 'E'.
RETURN.
ENDTRY.
" Step 5: Access the Table using a Field Symbol
ASSIGN gr_dynamic_table->* TO <gt_dynamic_table>.
IF <gt_dynamic_table> IS NOT ASSIGNED.
MESSAGE 'Could not assign dynamic table to field symbol.' TYPE 'E'.
ENDIF.
ENDMETHOD.
METHOD fill_dynamic_table.
" Check if the table was created and assigned successfully
IF <gt_dynamic_table> IS NOT ASSIGNED.
RETURN.
ENDIF.
FIELD-SYMBOLS:
<ls_wa> TYPE any, " Field symbol for the work area
<lv_field> TYPE any. " Field symbol for a single field/component
DO 5 TIMES. " Create 5 rows of data
" Create a new line in the dynamic table and get a reference to it
APPEND INITIAL LINE TO <gt_dynamic_table> ASSIGNING <ls_wa>.
" --- Fill the fixed column ---
ASSIGN COMPONENT 'ID' OF STRUCTURE <ls_wa> TO <lv_field>.
IF <lv_field> IS ASSIGNED.
<lv_field> = |PROD-{ sy-index }|.
ENDIF.
" --- Fill the dynamic columns ---
DO p_months TIMES.
DATA(lv_col_name) = |MONTH_{ sy-index }|.
ASSIGN COMPONENT lv_col_name OF STRUCTURE <ls_wa> TO <lv_field>.
IF <lv_field> IS ASSIGNED.
<lv_field> = sy-index * 100 + sy-tabix. " Some dummy data
ENDIF.
ENDDO.
ENDDO.
ENDMETHOD.
METHOD display_dynamic_table.
IF <gt_dynamic_table> IS ASSIGNED.
" Use the simple CL_DEMO_OUTPUT class for display
cl_demo_output=>display( <gt_dynamic_table> ).
ENDIF.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
NEW lcl_main( )->run( ).
Method 2: The ALV Helper Class Approach (Simpler, if the goal is an ALV Display)
If you know the ultimate purpose of this dynamic table is to be displayed in an ALV Grid (using CL_SALV_TABLE or CL_GUI_ALV_GRID), you can use a helper class that does most of the RTTS work for you.
The process is:
- Build a Field Catalog: This is an internal table that describes the columns (name, type, length, etc.).
- Call the Static Method: Use
CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE.
- Access the Table: You get back a data reference, which you then assign to a field symbol, just like in the first method.
Code Example:
REPORT z_dynamic_alv_table.
PARAMETERS: p_cols TYPE i DEFAULT 4. " Number of dynamic columns
START-OF-SELECTION.
" Step 1: Build the Field Catalog
DATA: lt_fieldcat TYPE lvc_t_fcat,
ls_fieldcat TYPE lvc_s_fcat.
" --- Add a fixed column ---
ls_fieldcat-fieldname = 'CARRID'.
ls_fieldcat-datatype = 'CHAR'.
ls_fieldcat-intlen = 3.
ls_fieldcat-coltext = 'Airline Code'.
APPEND ls_fieldcat TO lt_fieldcat.
CLEAR ls_fieldcat.
" --- Add dynamic columns ---
DO p_cols TIMES.
ls_fieldcat-fieldname = |VALUE_{ sy-index }|.
ls_fieldcat-datatype = 'INT4'.
ls_fieldcat-coltext = |Value { sy-index }|.
APPEND ls_fieldcat TO lt_fieldcat.
CLEAR ls_fieldcat.
ENDDO.
" Step 2: Call the static method to create the table
DATA: r_dyntable TYPE REF TO data.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_fieldcat
IMPORTING
ep_table = r_dyntable.
" Step 3: Access the table with a field symbol
FIELD-SYMBOLS: <fs_table> TYPE STANDARD TABLE.
ASSIGN r_dyntable->* TO <fs_table>.
" Now you can work with <fs_table>
IF <fs_table> IS ASSIGNED.
" Fill with some data (similar to Method 1)
FIELD-SYMBOLS: <fs_wa> TYPE any,
<fs_fld> TYPE any.
APPEND INITIAL LINE TO <fs_table> ASSIGNING <fs_wa>.
ASSIGN COMPONENT 'CARRID' OF STRUCTURE <fs_wa> TO <fs_fld>.
<fs_fld> = 'LH'.
ASSIGN COMPONENT 'VALUE_1' OF STRUCTURE <fs_wa> TO <fs_fld>.
<fs_fld> = 100.
" Display the result using ALV
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(lo_alv)
CHANGING
t_table = <fs_table>
).
lo_alv->display( ).
CATCH cx_salv_msg.
MESSAGE 'Error creating ALV' TYPE 'E'.
ENDTRY.
ENDIF.
Which Method to Choose?