2020 Mar 10 2:02 PM
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
2020 Mar 10 2:13 PM
Search RTTS in the forum, especially CL_ABAP_STRUCTDESCR=>CREATE or GET and CL_ABAP_TABLEDESCR=>CREATE or GET.
2020 Mar 10 2:18 PM
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
2020 Mar 10 3:21 PM
Hello All,
Thank for your response. Sample code would really helpful.
2020 Mar 10 3:51 PM
2022 Mar 12 7:45 AM
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.