Skip to end of metadata
Go to start of metadata

Author: Anirban Bhattacharjee
Submitted: 16 Sept 2008

Related Links:

Concept of Run Time Type Creation

Complete code

Type object creation

Type object can be created referring to the corresponding description class in 3 steps.

 Step 1:

Declaration of structure and table type referring to the corresponding description classes.  

ref_rowtype   TYPE REF TO cl_abap_structdescr ---> Structure declaration

ref_tabletype TYPE REF TO cl_abap_tabledescr  ---> Internal Table declaration 

Step 2:

Dynamic instantiation. First we create a line type dynamically referring to the given data table name by the syntax,

ref_rowtype   ?= cl_abap_typedescr=>describe_by_name( p_tab ).

After creation of the row type the same is widening casted to ref_rowtype. 

To create an internal table dynamically at runtime we use CREATE method of description class CL_ABAP_TABLEDESCR. CREATE method contains parameter  P_LINE _TYPE to determine line type of the internal table with respect to ref_rowtype defined above. Finally the internal table instantiated to ref_tabletype.

 ref_tabletype  = cl_abap_tabledescr=>create( p_line_type = ref_rowtype ).

Step 3:

Creation of object.

     The CREATE DATA allows us create object during run time and HANDLE addition is used as the reference is also created dynamically during runtime. CREATE DATA ref_itab  TYPE HANDLE ref_tabletype.

CREATE DATA ref_wa    TYPE HANDLE ref_rowtype.

 Value Assignment. 

 The value of  is assigned to the compatible field symbol by ASSIGN statement

 ASSIGN ref_itab->* TO <fsym_itab>.

 ASSIGN ref_wa->*   TO <fsym_warea>.  

Data Population into the dynamically created internal table.

SELECT *

  FROM (p_tab)

  INTO TABLE <fsym_itab>.

Display of data.

      The data is displayed in two steps.

      Initially we take data of each row of the internal table into work area one-by-one with the help of LOOP... ENDLOOP statement.

LOOP AT <fsym_itab> INTO <fsym_warea>.

   Inside this LOOP.... ENDLOOP, we take one DO...ENDDO. loop where the field values of each of the work area take previously is assigned to the field symbol <fsym_field>.

ASSIGN COMPONENT sy-index OF STRUCTURE <fsym_warea> TO <fsym_field>.

   After the value of the last field of each work area is assigned to <fsym_field>, if we again execute  the ASSIGN COMPONENT , the sy-subrc  value will become non-zero and we need to break the loop by EXIT statement.

     IF sy-subrc NE 0.

        NEW-LINE.

        EXIT.

      ENDIF.                

  In the selection screen , enter the name of any SAP Data table. Here I have taken SCARR.


On hit F8, we can get the contain of SCARR.