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

Build dynamic structure from field list internal table

bikash
Explorer
0 Likes
9,735

Hello All,

I have requirement.

Internal table itab1 value

fieldname fieldvalue

PERNR 400000001

BEGDA 99991231

ENDDA 99991231

I want to build a structure like PERNR BEGDA ENDDA..this will be dynamic. If the field list increases, the structure will extend.

Thanks

Hello All,

I have requirement.

Internal table itab1 value

fieldname fieldvalue

PERNR 400000001

BEGDA 99991231

ENDDA 99991231

I want to build a structure like PERNR BEGDA ENDDA..this will be dynamic. If the field list increases, the structure will extend.

Thanks

5 REPLIES 5
Read only

Sandra_Rossi
Active Contributor
7,651

Search RTTS in the forum, especially CL_ABAP_STRUCTDESCR=>CREATE or GET and CL_ABAP_TABLEDESCR=>CREATE or GET.

Read only

Dominik_Tylczynski
SAP Champion
SAP Champion
0 Likes
7,651

You can build an internal table of a dynamic structure with:

CREATE DATA ... HANDLE ...

See SAP Help: CREATE DATA - Quick reference and example of how to create a dynamic structure with ABAP RTTS classes: Creating a Structure Using RTTC

Here you'll find a description of RTTS classes: RTTS - Runtime Type Services There you'll find CL_ABAP_TABLEDESCR class that allows to dynamically create an internal table description.

Dominik Tylczynski

Read only

bikash
Explorer
0 Likes
7,651

Hello All,

Thank for your response. Sample code would really helpful.

Read only

0 Likes
7,651

There are already some samples in wiki part of the forum, use the method provided by Sandra as a keyword.

Read only

Preetha33
Explorer
7,651

Hi,

Below is the sample code for creating internal table dynamically.

DATA: lo_structtype TYPE REF TO cl_abap_structdescr, 
      lo_tabletype  TYPE REF TO cl_abap_tabledescr, 
      lo_dataref    TYPE REF TO data.

DATA: lt_comp TYPE cl_abap_structdescr=>component_table,
      ls_comp LIKE LINE OF lt_comp.

FIELD-SYMBOLS: <lt_table> TYPE STANDARD TABLE, 
              <ls_table> TYPE any,
               <ls_val>   TYPE any.

ls_comp-name = 'PERNR'.
ls_comp-type ?= cl_abap_datadescr=>describe_by_name( 'CHAR20'  ).
APPEND ls_comp TO lt_comp.

ls_comp-name = 'BEGDA'.
ls_comp-type ?= cl_abap_datadescr=>describe_by_name( 'CHAR20'  ).
APPEND ls_comp TO lt_comp.

ls_comp-name = 'ENDDA'.
ls_comp-type ?= cl_abap_datadescr=>describe_by_name( 'CHAR20' ).
APPEND ls_comp TO lt_comp.

lo_structtype ?= cl_abap_structdescr=>create( lt_comp ).
lo_tabletype = cl_abap_tabledescr=>create(                              
p_line_type   = lo_structtype                              
p_table_kind  = cl_abap_tabledescr=>tablekind_STD                              
p_unique      = abap_false                             
 p_key         = VALUE #( ( NAME = 'PERNR' ) )                             
 p_key_kind    = cl_abap_tabledescr=>keydefkind_user ).

CREATE DATA lo_dataref TYPE HANDLE lo_tabletype.
ASSIGN lo_dataref->* TO <lt_table>.
CREATE DATA lo_dataref TYPE HANDLE lo_structtype.
ASSIGN lo_dataref->* TO <ls_table>.  

ASSIGN COMPONENT 'PERNR'  OF STRUCTURE <ls_table> TO <ls_val>.  
<ls_val> = ' 400000001'. 

ASSIGN COMPONENT 'BEGDA' OF STRUCTURE <ls_table> TO <ls_val>.
  <ls_val> = ' 99991231'. 

ASSIGN COMPONENT 'ENDDA' OF STRUCTURE <ls_table> TO <ls_val>.  
<ls_val> = ' 99991231'.  
INSERT <ls_table> INTO TABLE <lt_table>.

cl_demo_output=>display( <lt_table> ).

Thanks & Regards,

Preetha.