ABAP: create a table from scratch using RTTS

RTTS: RunTime Type Services Рallows to get the definition of variables or to create them during program execution. RTTS is made of 2 components:

  • RTTI: RunTime Type Identification – is used to get the definition of existing variables or existing types
  • RTTC: RunTime Type Creation – is used to create new variables with any definition; they must be followed by the ABAP statement CREATE DATA … TYPE HANDLE … to create the variable

RTTI and RTTC may be called using methods in CL_ABAP_*DESCR classes. Each class have both RTTI and RTTC methods.

Here is a quick example that illustrates the power and elegance of creating a table:

DATA: lo_lineType   TYPE REF TO cl_abap_structdescr,
      lo_tableType  TYPE REF TO cl_abap_tabledescr,
      lt_key        TYPE abap_keydescr_tab.

lo_lineType ?= cl_abap_typedescr=>describe_by_name( 'SPFLI' ).
APPEND 'CARRID' TO lt_key.
APPEND 'CONNID' TO lt_key.

lo_tableType = cl_abap_tabledescr=>create(
                  p_line_type   = lo_lineType
                  p_table_kind  = cl_abap_tabledescr=>tablekind_sorted
                  p_unique      = abap_true
                  p_key         = lt_key ).

* now lets create some data
DATA: lo_itab TYPE REF TO data.
CREATE DATA lo_itab TYPE HANDLE lo_tableType.

As you can see the steps are quite simple and the result is a standard SPFLI table.

Leave a Reply

*

captcha *