Dynamic internal Table A Dynamic Internal Table is an internal table with variable number of rows and columns, which can be defined during run time only.
A dynamic internal table is not declared in the program as static.
Some of the benefits of Dynamic internal table are:
- Extremely useful when the numbers of columns / fields are not known at the design time / compile time.
- Avoids redundancy
Some of the important attributes of an internal table which can be set dynamically are
- Column position
- Field name of internal table field
- Column width
- Reference field
- Reference table
- Data type of the fields
- Domain name of the fields
- Check table etc.
(For the remaining attributes please refer the ABAP structure LVC_S_FCAT) Prerequisites of dynamic internal table are
Knowledge of field symbols
Knowledge on data references
Field symbols are placeholders or symbolic names for other fields. They are similar to dereference pointers in C.
Field symbols allow you to:
Assign an alias to a data object
Adopt or change the type and size of a data object dynamically at runtime
Set the offset and length for a string variably at runtime
Set a pointer to a data object that you determine at runtime (dynamic ASSIGN)
Access components of a structure
The statement ASSIGN f to <fs> assigns the field f to field symbol <fs>. The field symbol <fs> then "points" to the contents of field f at runtime. This means that all changes to the contents of f are visible in <fs> and vice versa.
You declare the field symbol <fs> using the statement FIELD-SYMBOLS: <fs>.
Data references are pointers to data objects.
You can use data references to create data objects dynamically. You can also create references to existing data objects.
You can only dereference a data reference using a special assignment to a field symbol.
You can create a data reference variable by using:
DATA <dref> TYPE REF TO DATA.
To create a data object dynamically during a program, you need a data reference variable and the following statement:
CREATE DATA <dref> TYPE <type>|LIKE <obj>.
To access the contents of the data object to which a data reference is pointing, you must dereference it.
ASSIGN <dref>->* TO <FS>. Dynamic internal tables can be created using :
- The class CL_ALV_TABLE_CREATE and the method CREATE_DYNAMIC_TABLE.
Export parameter: it_fieldcatalog
Import parameter: ep_table
Exceptions: generate_subpool_dir_full = 1
Others = 2 Structure for Dynamic Internal Table Creation
- ABAP Structure LVC_S_FCAT
This structure is used to maintain the attributes of each field of the Dynamic
Internal Table such as the fieldname, column position, etc.
- ABAP Table type LVC_T_FCAT
This table type has a line type of LVC_S_FCAT. The field attributes of all the
Fields are maintained in this table Steps to create a Dynamic Internal Table
- Data Definitions
Declare a structure of type lvc_s_fcat.
Declare an internal table of type lvc_t_fcat (The line type of this internal table is lvc_s_fcat).
Declare two data reference variables, one for the dynamic internal table (say dr1) and the other for the work area (say dr2)
Declare field symbols of type 'ref to data', 'any table' and of type 'any' (say fs1, fs2 and fs3 respectively). # Populate the internal table with fieldnames required for the dynamic internal table
a).Assign the field name, field type, field width, check table etc. to the structure.
b).Append the structure to the internal table # Assign Field-Symbol to dynamic internal table
i.e. Assign dr1 to <fs1> # Call the method CREATE_DYNAMIC_TABLE
a).Pass the internal table containing the field attributes as the export parameter.
b).Pass the field symbol of the dynamic internal table as the import parameter. # This creates the dynamic internal table
<fs1> now refers to dynamic internal table that we wanted to create at start. # Assign the data contents of <fs1> to a field-symbol <fs2> (dereferencing).
So <fs2> now points to the dynamic internal table. # Next step is to create a work area for our dynamic internal table.
Create data dr2 like line of <fs2>. # A field-symbol to access that work area
Assign dr2->* to <FS_2>. Drawbacks of a Dynamic Internal Table
- Programs with many dynamic internal tables are less readable and they are less
secure, since error cannot be detected by syntax check, but only by the runtime
- Performance when a Dynamic Internal Table is used will not be as good as, when a Static internal table is used.
Sample Program example: how to create a dynamic internal table