Skip to end of metadata
Go to start of metadata


Author: Anirban Bhattacharjee
Submitted: 10-Sept-2008
Related Links:

Concept of RTTC

Steps to Create Type Dynamically

This code snippet is meant to create an internal table dynamicaly, data population into it and display its contains as simple report.

RTTC, namely Run Time Type Creation is a concept of creating any data type at runtime. The feature is introduced in SAP Web Application Server 6.40

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
REPORT z_rttc.
PARAMETERS:
  p_tab TYPE dd02l-tabname .                  " Input data table name

FIELD-SYMBOLS:
  <fsym_itab> TYPE ANY TABLE,                 " Field symbol for internal table
  <fsym_warea> TYPE any,                      " Field symbol for work area
  <fsym_field> TYPE any.                      " Field symbol for field value

DATA:
  ref_rowtype
    TYPE REF TO cl_abap_structdescr,            " Rowtype ref to RTTS
  ref_tabletype
    TYPE REF TO cl_abap_tabledescr.             " Internal table type ref to RTTS

DATA:
  ref_wa TYPE REF TO data,           " Declaration of work area instance
  ref_itab TYPE REF TO data.                  " Declaration of internal table instance


*"---------------------------------------------------------------------
* START-OF-SELECTION
*"---------------------------------------------------------------------

START-OF-SELECTION.

* Create row type and tabletype by RTTC technique
  ref_rowtype ?= cl_abap_typedescr=>describe_by_name( p_tab ).
  ref_tabletype = cl_abap_tabledescr=>create( p_line_type = ref_rowtype ).

* Object type created by RTTC.
  CREATE DATA ref_itab TYPE HANDLE ref_tabletype.
  CREATE DATA ref_wa TYPE HANDLE ref_rowtype.

*Type assignment
  ASSIGN ref_itab->* TO <fsym_itab>.
  ASSIGN ref_wa->* TO <fsym_warea>.

* Value assignment to the internal table
  SELECT *
    FROM (p_tab)
    INTO TABLE <fsym_itab>.

* Display Output
  LOOP AT <fsym_itab> INTO <fsym_warea>.
    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE <fsym_warea> TO
      <fsym_field>.
      IF sy-subrc NE 0.
        NEW-LINE.                           " Shift Cursor to next line
        EXIT.                               " EXIT to next work area
      ENDIF.                              " IF sy-subrc NE 0.
      WRITE <fsym_field>.
    ENDDO.
  ENDLOOP.                            " LOOP AT <fsym_itab> INTO <fsym_warea>.

1 Comment

  1. Hi,

    How would you proceed to achieve similar goal before 6.40?

    Thanks in advance,
    Guillaume