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

dynamic creation of structure

Former Member
0 Likes
1,895

Hi everybody,

my requirement is to create an internal table depending on a dynamic type.

then create another itab wich have the same structure but with different types (char50).

exemple :

parameters p_tab type dd02l-tabname.

data itab ref to data.

create data itab type table of (p_tab).

in this case, if user enter bseg as parameter, my itab will have this structure: mandt(clnt3)   bukrs(char4)   belnr(char10)  gjahr(numc4) ....

my requirement is to create an itab2 with this structure like this:                    mandt(char50)  bukrs(char50)   belnr(char50)   gjahr(char50) ....

thanks.

1 ACCEPTED SOLUTION
Read only

SharathYaralkattimath
Contributor
0 Likes
1,857

Hi Raj,

You could use RTTS & classes related to RTTI.

I have tested this code which will fulfill your purpose.


PARAMETERS p_tab TYPE dd02l-tabname.

DATA itab TYPE REF TO data.

CREATE DATA itab TYPE TABLE OF (p_tab).

DATA:

  wa_structure    TYPE REF TO data,

  li_table        TYPE REF TO data,

  lr_field_descr  TYPE REF TO cl_abap_elemdescr,

  lo_structure    TYPE REF TO cl_abap_structdescr,

  lo_table        TYPE REF TO cl_abap_tabledescr,

  ls_component    TYPE cl_abap_structdescr=>component,

  li_tot_comp     TYPE cl_abap_structdescr=>component_table.

DATA lr_structdescr TYPE REF TO  cl_abap_structdescr.

DATA l_component    TYPE abap_compdescr .

lr_structdescr ?= cl_abap_typedescr=>describe_by_name( p_tab ).

LOOP AT lr_structdescr->components INTO l_component.

  ls_component-name = l_component-name.

  lr_field_descr   ?= cl_abap_elemdescr=>describe_by_name( 'CHAR50' ).

  ls_component-type = lr_field_descr.

  INSERT ls_component INTO TABLE li_tot_comp.

ENDLOOP.

*Create data

lo_structure    = cl_abap_structdescr=>create( li_tot_comp ).

lo_table        = cl_abap_tabledescr=>create( lo_structure ).

* Create dynamic objects

CREATE DATA wa_structure TYPE HANDLE lo_structure.

CREATE DATA li_table TYPE HANDLE lo_table.

Test this code & let me know your comments,

Thanks,

Sharath

Hi everybody,

my requirement is to create an internal table depending on a dynamic type.

then create another itab wich have the same structure but with different types (char50).

exemple :

parameters p_tab type dd02l-tabname.

data itab ref to data.

create data itab type table of (p_tab).

in this case, if user enter bseg as parameter, my itab will have this structure: mandt(clnt3)   bukrs(char4)   belnr(char10)  gjahr(numc4) ....

my requirement is to create an itab2 with this structure like this:                    mandt(char50)  bukrs(char50)   belnr(char50)   gjahr(char50) ....

thanks.

6 REPLIES 6
Read only

SharathYaralkattimath
Contributor
0 Likes
1,858

Hi Raj,

You could use RTTS & classes related to RTTI.

I have tested this code which will fulfill your purpose.


PARAMETERS p_tab TYPE dd02l-tabname.

DATA itab TYPE REF TO data.

CREATE DATA itab TYPE TABLE OF (p_tab).

DATA:

  wa_structure    TYPE REF TO data,

  li_table        TYPE REF TO data,

  lr_field_descr  TYPE REF TO cl_abap_elemdescr,

  lo_structure    TYPE REF TO cl_abap_structdescr,

  lo_table        TYPE REF TO cl_abap_tabledescr,

  ls_component    TYPE cl_abap_structdescr=>component,

  li_tot_comp     TYPE cl_abap_structdescr=>component_table.

DATA lr_structdescr TYPE REF TO  cl_abap_structdescr.

DATA l_component    TYPE abap_compdescr .

lr_structdescr ?= cl_abap_typedescr=>describe_by_name( p_tab ).

LOOP AT lr_structdescr->components INTO l_component.

  ls_component-name = l_component-name.

  lr_field_descr   ?= cl_abap_elemdescr=>describe_by_name( 'CHAR50' ).

  ls_component-type = lr_field_descr.

  INSERT ls_component INTO TABLE li_tot_comp.

ENDLOOP.

*Create data

lo_structure    = cl_abap_structdescr=>create( li_tot_comp ).

lo_table        = cl_abap_tabledescr=>create( lo_structure ).

* Create dynamic objects

CREATE DATA wa_structure TYPE HANDLE lo_structure.

CREATE DATA li_table TYPE HANDLE lo_table.

Test this code & let me know your comments,

Thanks,

Sharath

Read only

0 Likes
1,857

Hi Sharath,

Thank you very much for your answer.

I tried your code using BSEG as parameter

I runned it in debug mode and the result was like this:

But it was supposed to be like this (but with char50 as type):

because after creating this dynamic itab i have to fill it by from an excel file by calling the FM 'TEXT_CONVERT_XLS_TO_SAP'

Read only

0 Likes
1,857

CHECK wa_structure AND li_table.

These are the work area and internal table created.

Read only

0 Likes
1,857

I am new at RTTI and RTTS programming so sorry for my ignorance .

can you tell me how to fill this kind of table (li_table)?

and then how to select from it?

thank you in advance.

Read only

0 Likes
1,857

FIELD-SYMBOLS : <fs_it> TYPE STANDARD TABLE,    "Dynamic intenal table

                                <fs_wa>,                             "Dynamic work area

                                <fs_fvalue>  TYPE any.         "Component in work area


ASSIGN wa_structure->*    TO <fs_wa>.           "Dynamic work area

ASSIGN li_table->*            TO <fs_it>.              "Dynamic Internal Table


To fill data into work area...

ASSIGN COMPONENT 'BUKRS' OF STRUCTURE <fs_wa> TO <fs_fvalue>.

<fs_fvalue> = '1000'. "Company code

Append work area to itab.

APPEND <FS_WA> TO <FS_IT>.

Read table

READ TABLE <fs_it> INTO <fs_wa_1> WITH KEY ('BUKRS') = '1000'.

Read only

alessandroieva
Active Participant
0 Likes
1,857