Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Determining internal table structure dynamically

Former Member
0 Likes
8,111

Hi,

I have a number of internal tables in my program which I declare using types. As an example:

TYPES: begin of ty_hierarchy,

control_id(6) type c,

node_id(10) type c,

node_name type bezei40,

material type matnr,

node_level type prodh_stuf,

node_parent(10) type c,

end of ty_hierarchy.

DATA: it_hierarchy type ty_hierarchy occurs 0.

Further down my program I need to determine the structure of internal table IT_HIERARCHY dynamically. Because I have a number of internal tables, I need to determine which internal table is being processed. Therefore it's important that I know the structure of the table that I'm currently processing.

I am aware of CL_ABAP* classes and functions like GET_COMPONENT_LIST. However because I have declared my tables using the TYPE statement the method/function cannot read my table structure correctly. If I changed my declaration to be as below, the method/function work! However I don't want to do this as I use field symbols to reference my internal tables and need to use the TYPE statement.

DATA: begin of ty_hierarchy,

control_id(6) type c,

node_id(10) type c,

node_name type bezei40,

material type matnr,

node_level type prodh_stuf,

node_parent(10) type c,

end of ty_hierarchy.

DATA: begin of it_hierarchy occurs 0.

include structure ty_hierarchy

DATA: end of it_hierarchy.

Does anyone know on how I can determine my internal table structure dynamically but still keeping my internal table declarations using TYPE statement?

Any help would be greatly appreciated with reward points .....

Thanks

Liam

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,168

Hi Liam

Please check below sample code which might help you:

types: begin of t_dat,
         matnr type matnr,
         werks type werks,
         fld1(10) type c,
       end of t_dat.

data: it_dat type standard table of t_dat,
      wa_dat type t_dat.

data: it_comp type table of RSTRUCINFO.
CALL FUNCTION 'GET_COMPONENT_LIST'
  EXPORTING
    PROGRAM          = sy-repid
    FIELDNAME        = 'WA_DAT'
  TABLES
    COMPONENTS       = it_comp.
  

Kind Regards

Eswar

2 REPLIES 2
Read only

Former Member
0 Likes
2,169

Hi Liam

Please check below sample code which might help you:

types: begin of t_dat,
         matnr type matnr,
         werks type werks,
         fld1(10) type c,
       end of t_dat.

data: it_dat type standard table of t_dat,
      wa_dat type t_dat.

data: it_comp type table of RSTRUCINFO.
CALL FUNCTION 'GET_COMPONENT_LIST'
  EXPORTING
    PROGRAM          = sy-repid
    FIELDNAME        = 'WA_DAT'
  TABLES
    COMPONENTS       = it_comp.
  

Kind Regards

Eswar

Read only

uwe_schieferstein
Active Contributor
0 Likes
2,168

Hello Liam

Both the ABAP-OO as well as the FM-based approach described by Eswar work well with your way of defining the itabs. I described three different ways how to get the structure of your itab dynamically:

- directly using the itab

- using a field symbol

- using a data reference

*&---------------------------------------------------------------------*

REPORT  zus_sdn_dynamic_itabs.

TYPE-POOLS: abap.


TYPES: BEGIN OF ty_hierarchy,
control_id(6) TYPE c,
node_id(10) TYPE c,
node_name TYPE bezei40,
material TYPE matnr,
node_level TYPE prodh_stuf,
node_parent(10) TYPE c,
END OF ty_hierarchy.

DATA:
  gs_hierarchy    TYPE ty_hierarchy,
  it_hierarchy    TYPE ty_hierarchy OCCURS 0.

DATA:
  gt_comp         TYPE abap_compdescr_tab,
  gs_comp_a       LIKE LINE OF gt_comp,
  gd_type         TYPE abap_typekind,

  gs_comp    TYPE rstrucinfo,
  it_comp TYPE TABLE OF rstrucinfo.


DATA:
  go_struct    TYPE REF TO cl_abap_structdescr,
  go_table     TYPE REF TO cl_abap_tabledescr,
  gdo_data     TYPE REF TO data.

FIELD-SYMBOLS:
  <gt_itab>    TYPE table.



START-OF-SELECTION.


  GET REFERENCE OF it_hierarchy INTO gdo_data.
  ASSIGN gdo_data->* TO <gt_itab>.

* (1) Describe directly by using the itab
*  go_table  ?= cl_abap_structdescr=>describe_by_data( it_hierarchy ).
* (2) Describe indirectly by using field symbol
*  go_table  ?= cl_abap_structdescr=>describe_by_data( <gt_itab> ).
* (3) Describe by data reference to itab
  go_table  ?= cl_abap_structdescr=>describe_by_data_ref( gdo_data ).
  go_struct ?= go_table->get_table_line_type( ).

  WRITE: / 'ABAP-OO Version:'.
  gt_comp = go_struct->components.
  LOOP AT gt_comp INTO gs_comp_a.
    WRITE: / gs_comp_a-name,
             gs_comp_a-length,
             gs_comp_a-type_kind,
             gs_comp_a-decimals.
  ENDLOOP.
  SKIP 2.




  CALL FUNCTION 'GET_COMPONENT_LIST'
    EXPORTING
      program    = sy-repid
      fieldname  = 'GS_HIERARCHY'
    TABLES
      components = it_comp.

  WRITE: / 'Function Module Version:'.
  LOOP AT it_comp INTO gs_comp.
    WRITE: / gs_comp-compname,
             gs_comp-level,
             gs_comp-leng,
             gs_comp-type,
             gs_comp-olen,
             gs_comp-decs.

  ENDLOOP.

END-OF-SELECTION.

Regards

Uwe