You are on page 1of 5

Dynamic internal table creation using RTTC

It is a very common requirement to create an internal table


dynamically where the structure of the internal table will not be predefined and the table structure will be decided at run time.
Old Process:
As a solution to this requirement, generally the method
CREATE_DYNAMIC_TABLE of the class CL_ALV_TABLE_CREATE is
used where an internal table can be passed as the Field Catalog and it
returns a Pointer to Dynamic Data Table.
Steps for old logic:
1. Create the field catalog first to populate an internal table of type
LVC_T_FCAT
DATA:
lt_fcat TYPE lvc_t_fcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name
= 'MAKT'
i_client_never_display = 'X'
CHANGING
ct_fieldcat
= lt_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error
= 2
OTHERS
= 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

2. Call the method to create the dynamic internal table


** Data reference declarations
DATA:
lref_ditab
TYPE REF TO data,
lref_new_line TYPE REF TO data.
** Field-Symbols declarations
FIELD-SYMBOLS:
<fs_dyn_tab1> TYPE ANY TABLE,
<fs_dyn_wa>
TYPE ANY.

** Field Catalog declarations


DATA:
lt_fcat
TYPE lvc_t_fcat.
** Create dynamic table

CALL METHOD cl_alv_table_create=>create_dynamic_table


EXPORTING
it_fieldcatalog
= lt_fcat
IMPORTING
ep_table
= lref_ditab
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS
= 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
** Assign the dynamic table reference to a field-symbol
ASSIGN lref_ditab->* TO <fs_dyn_tab1>.
** Create a structure similar to the dynamic table created
CREATE DATA lref_new_line LIKE LINE OF <fs_dyn_tab1>.
ASSIGN lref_new_line->* TO <fs_dyn_wa>.

Problems with the old logic:


Problem will be faced, if this method needs to be used repeatedly
in the same program to create multiple internal tables (for example: if
the method is used inside a loop). It starts giving errors / ABAP dumps
after certain iterations.
While investigating the issue, it was found that the method is
using the statement GENERATE SUBROUTINE POOL in its underneath
logic; this statement has a specific limitation - in an internal mode, a
maximum of 36 temporary subroutine pools can be created (as per
SAP-help). As a result of this, the method starts raising exceptions after
its 36th execution.
New Process:
SAP has introduced a new concept known as RTTC (Run Time
Type Creation) since Release 6.40. This is a concept of creating any
Data Type dynamically at runtime using the methods available in
system classes (Description classes) of RTTS (Run Time Type
Services).
Mostly we will be using the following description classes:
CL_ABAP_TYPEDESCR (determine and generate type
attributes)
CL_ABAP_ELEMDESCR (describe and generate elementary
data types)
CL_ABAP_STRUCTDESCR (describe and create structures)
CL_ABAP_TABLEDESCR (describe and generate internal tables)
Steps for new logic:
1. Declaration of structure and table
corresponding description classes.

type

referring

to

the

DATA:
lref_rowtype TYPE REF TO cl_abap_structdescr, "Structure
lref_tabtype TYPE REF TO cl_abap_tabledescr. "Internal Table

2. The line type can be created dynamically. This can be done in


several ways:
a. If you have a database structure / table for this purpose,
you can directly use that as the input parameter for the
method
DESCRIBE_BY_NAME
of
the
class
CL_ABAP_TYPEDESCR. After creation of the row type the
same is widening casted to lref_rowtype.
lref_rowtype

?= cl_abap_typedescr=>describe_by_name( 'MAKT' ).

b. If you dont have that, you can create the component table
first; and then you can use that component table as an
input parameter for the method CREATE of the class
CL_ABAP_STRUCTDESCR to get the Line type reference in
lref_rowtype.
** Prepare the Component Table
DATA:
wa_comp TYPE abap_componentdescr,
lt_comp TYPE abap_component_tab.
wa_comp-name = 'MATNR'.
wa_comp-type ?= cl_abap_elemdescr=>describe_by_name( 'MATNR' ).
wa_comp-name = 'MAKTX'.
wa_comp-type ?= cl_abap_elemdescr=>describe_by_name( 'MAKTX' ).
APPEND wa_comp TO lt_comp.
CLEAR wa_comp.
** Get the line type
lref_rowtype = cl_abap_structdescr=>create( p_components = lt_comp ).

3. To create an internal table dynamically at runtime we use


CREATE method of description class CL_ABAP_TABLEDESCR.
This method contains parameter P_LINE_TYPE to determine line
type of the internal table with respect to lref_rowtype defined
above.
Finally
the
internal
table
is
instantiated
to
lref_tabletype.
lref_tabtype = cl_abap_tabledescr=>create( p_line_type = lref_rowtype
).

4. The CREATE DATA allows creating object during run time and
HANDLE addition is used as the reference is also created
dynamically during runtime.

DATA:
lref_ditab
TYPE REF TO data,
lref_new_line TYPE REF TO data.
CREATE DATA: lref_ditab
TYPE HANDLE lref_tabtype,
lref_new_line TYPE HANDLE lref_rowtype.

5. The dynamic table / structure reference is assigned to the


compatible field symbols.
FIELD-SYMBOLS:
<fs_dyn_tabl>
<fs_dyn_wa>

TYPE ANY TABLE,


TYPE ANY.

ASSIGN lref_ditab->*
TO <fs_dyn_tabl>.
ASSIGN lref_new_line->* TO <fs_dyn_wa>.

Example:
Here is an example Enter any table name in the Selection
screen and the Program will display all the field values of that table.
1. Attached is a sample code that can be used as a ready reference
to test this example.
Sample Programfor
RTTC

2. Sample input and the corresponding result.