I am a SAP ABAP developer. I want to create an internal table in an ABAP report dynamically. The amount of columns differs depending on the values that are provided. How can I do this?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To dynamically create an internal table in ABAP with a varying number of columns, you can use the CL_ABAP_STRUCTDESCR class to build a structure at runtime and then use field symbols or generic tables to handle the data. Here's a step-by-step guide:
Step-by-Step Explanation
Determine Columns Dynamically
Based on input values, decide the fields (e.g., MATNR, WERKS, etc.) and their types.
Create a Dynamic Structure
Use CL_ABAP_STRUCTDESCR to generate a structure description at runtime.
Create Data References
Use CREATE DATA to create a data reference for the structure and the internal table.
Use Field Symbols for Access
Assign the data reference to a field symbol to manipulate the structure and internal table dynamically.
Example Code
abap
DATA: lr_structure TYPE REF TO cl_abap_structdescr,
lr_data TYPE REF TO data,
lr_table TYPE REF TO data,
ls_table TYPE any,
lt_table TYPE STANDARD TABLE OF any.
" Step 1: Create a dynamic structure
lr_structure = cl_abap_structdescr=>create( ).
" Add fields to the structure (example: MATNR and WERKS)
lr_structure->add_field( name = 'MATNR' type = 'CHAR' length = 18 ).
lr_structure->add_field( name = 'WERKS' type = 'CHAR' length = 4 ).
" Step 2: Create data references for the structure and internal table
CREATE DATA lr_data TYPE HANDLE lr_structure.
CREATE DATA lr_table TYPE STANDARD TABLE OF (lr_data->get_type( )).
" Step 3: Assign data to field symbols
ASSIGN lr_data->* TO FIELD-SYMBOL(<fs_structure>).
ASSIGN lr_table->* TO FIELD-SYMBOL(<fs_table>).
" Step 4: Populate the internal table
DATA: ls_temp TYPE any.
" Example: Add a row to the internal table
CREATE DATA ls_temp TYPE HANDLE lr_structure.
ASSIGN ls_temp->* TO FIELD-SYMBOL(<fs_row>).
<fs_row>-MATNR = '123456789012345678'.
<fs_row>-WERKS = '1000'.
APPEND <fs_row> TO <fs_table>.
" Step 5: Access the internal table
LOOP AT <fs_table> ASSIGNING FIELD-SYMBOL(<fs_current>).
WRITE:/ <fs_current>-MATNR, <fs_current>-WERKS.
ENDLOOP.
Key Notes
Dynamic Structure: CL_ABAP_STRUCTDESCR allows you to create a structure with fields defined at runtime.
Data References: CREATE DATA is used to instantiate the structure and internal table.
Field Symbols: These enable dynamic access to the structure and table fields.
Limitations: Directly creating an internal table with a dynamic structure type is not supported in ABAP. Instead, use generic types (STANDARD TABLE OF any) and field symbols for flexibility.
Alternative Approach
If the structure is known at runtime but not predefined, use a generic internal table with a field catalog (e.g., LVC_S_FCAT) to dynamically handle columns. This is common in reports using REUSE_ALV_GRID_DISPLAY.
This approach provides flexibility for dynamically varying columns while adhering to ABAP's static typing constraints.