Skip to end of metadata
Go to start of metadata

Author: Uwe Schieferstein
Submitted: 04 August 2007
Related Links:

Description: The following report shows how to create dynamically flat and complex internal tables and how to fill them.

Error rendering macro 'code': Invalid value specified for parameter 'com.atlassian.confluence.ext.code.render.InvalidValueException'
*&---------------------------------------------------------------------*
*& Report  Z_RTTI_CREATE_COMPLEX_ITAB
*&
*&---------------------------------------------------------------------*
*& NOTE: revised version of ZUS_SDN_RTTI_CREATE_STRUCTURES
*&
*&---------------------------------------------------------------------*

REPORT  Z_RTTI_CREATE_COMPLEX_ITAB.


TYPE-POOLS: abap.


DATA:
  celltab          TYPE lvc_t_styl.  " becomes field of complex itab

DATA:
  gd_tabnam        TYPE string,
  gd_tabfield      TYPE string,
  go_table         TYPE REF TO cl_salv_table,
  go_sdescr        TYPE REF TO cl_abap_structdescr,
  go_sdescr_new    TYPE REF TO cl_abap_structdescr,
  go_tdescr        TYPE REF TO cl_abap_tabledescr,
  gdo_data         TYPE REF TO data,
  gdo_handle       TYPE REF TO data,
  gs_component     TYPE abap_compdescr,
  gs_comp          TYPE abap_componentdescr,
  gt_components    TYPE abap_component_tab.


FIELD-SYMBOLS:
  <gt_itab>     TYPE STANDARD TABLE.


PARAMETER:
  p_tabnam      TYPE tabname  DEFAULT 'KNB1'.

START-OF-SELECTION.

  " Describe structure
  go_sdescr ?= cl_abap_structdescr=>describe_by_name( p_tabnam ).
  gd_tabnam     = go_sdescr->get_relative_name( ).


  "   Simulate dynamic addition of columns to ALV list
  DO 5 TIMES.
    READ TABLE go_sdescr->components INTO gs_component INDEX syst-index.

    "   Build fieldname
    CONCATENATE gd_tabnam gs_component-name INTO gd_tabfield
                                            SEPARATED BY '-'.

    CLEAR: gs_comp.
    gs_comp-type ?= cl_abap_datadescr=>describe_by_name( gd_tabfield ).
    gs_comp-name  = gs_component-name.
    APPEND gs_comp TO gt_components.

     "   Create instances of dynamic structure and dynamic internal table
    go_sdescr_new  = cl_abap_structdescr=>create( gt_components ).
    go_tdescr      = cl_abap_tabledescr=>create( go_sdescr_new ).
    "   Create data refence followed by table creation
    CREATE DATA gdo_handle TYPE HANDLE go_tdescr.  " !!!
    ASSIGN gdo_handle->* TO <gt_itab>.

*   Dynamic select
    SELECT        * FROM  (p_tabnam)
      INTO CORRESPONDING FIELDS OF TABLE <gt_itab>
           WHERE  bukrs  = '2000'.

    TRY.
        CALL METHOD cl_salv_table=>factory
          IMPORTING
            r_salv_table = go_table
          CHANGING
            t_table      = <gt_itab>.
        go_table->display( ).
      CATCH cx_salv_msg .
    ENDTRY.
  ENDDO.


  " Display component list in order to prove that indeed the field names
  " are used (instead of the data element names)
  TRY.
      CALL METHOD cl_salv_table=>factory
        IMPORTING
          r_salv_table = go_table
        CHANGING
          t_table      = gt_components.
      go_table->display( ).
    CATCH cx_salv_msg .
  ENDTRY.



  " Add table type (LVC_T_STYL) as field to structure ==> complex structure
  CLEAR: gs_comp.
  gs_comp-type ?= cl_abap_typedescr=>describe_by_data( celltab ).
  gs_comp-name  = 'CELLTAB'.
  APPEND gs_comp TO gt_components.

  go_sdescr  = cl_abap_structdescr=>create( gt_components ).
  go_tdescr  = cl_abap_tabledescr=>create( go_sdescr ).
  CREATE DATA gdo_handle TYPE HANDLE go_tdescr.
  ASSIGN gdo_handle->* TO <gt_itab>.

*   Dynamic select
  SELECT        * FROM  (p_tabnam)
    INTO CORRESPONDING FIELDS OF TABLE <gt_itab>
         WHERE  bukrs  = '2000'.


  " Show how to fill the complex itab
  PERFORM fill_celltab.

END-OF-SELECTION.


*&---------------------------------------------------------------------*
*&      Form  FILL_CELLTAB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fill_celltab .
* define local data
  DATA:
    ls_cell       TYPE lvc_s_styl,
    lt_celltab    TYPE lvc_t_styl.


  FIELD-SYMBOLS:
    <gs_struc>    TYPE ANY,
    <lt_celltab>  TYPE lvc_t_styl.


  " Create dummy entry for local CELLTAB
  ls_cell-fieldname = 'BUKRS'.
  ls_cell-style     = cl_gui_alv_grid=>mc_style_enabled.
  INSERT ls_cell INTO TABLE lt_celltab.


  LOOP AT <gt_itab> ASSIGNING <gs_struc>.
    ASSIGN COMPONENT 'CELLTAB' OF STRUCTURE <gs_struc> TO <lt_celltab>.

    <lt_celltab> = lt_celltab.

    "   No MODIFY required because we are working with the field symbol
  ENDLOOP.

ENDFORM.                    " FILL_CELLTAB