Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Creating Objects →  CREATE DATA →  CREATE DATA - HANDLE 

Creating a Structure Using RTTC

The example demonstrates how a structure is created using RTTC.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: struct_type TYPE REF TO cl_abap_structdescr,
          comp_tab    TYPE cl_abap_structdescr=>component_table,
          comp        LIKE LINE OF comp_tab,
          dref        TYPE REF TO data,
          oref        TYPE REF TO cx_sy_struct_creation.

    FIELD-SYMBOLS: <struc>  TYPE any,
                   <comp1>  TYPE any,
                   <comp2>  TYPE any.

    comp-name = column1.
    comp-type = cl_abap_elemdescr=>get_c( 40 ).
    APPEND comp TO comp_tab.

    comp-name = column2.
    comp-type = cl_abap_elemdescr=>get_i( ).
    APPEND comp TO comp_tab.


    TRY.
        struct_type = cl_abap_structdescr=>get( comp_tab ).
        CREATE DATA dref TYPE HANDLE struct_type.
      CATCH cx_sy_struct_creation INTO oref.
        MESSAGE oref TYPE 'I' DISPLAY LIKE 'E'.
        RETURN.
    ENDTRY.

    ASSIGN dref->* TO <struc>.
    ASSIGN COMPONENT column1 OF STRUCTURE <struc> TO <comp1>.
    <comp1> = 'Amount'.

    ASSIGN dref->* TO <struc>.
    ASSIGN COMPONENT column2 OF STRUCTURE <struc> TO <comp2>.
    <comp2> = 11.

    WRITE: / column1, <comp1>,
           / column2, <comp2>.

Description

Dynamic definition of a structure with two components using the GET method of the CL_ABAP_STRUCTDESCR class. The description of the components of the structure is provided in the internal table comp_tab. If no type object exists for this structure yet, it is created by the GET method.

The method CHECK_TABLE_NAME_STR of the class CL_ABAP_DYN_PRG checks whether the database table specified exists and can be used.