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 Structure Creation

Former Member
0 Likes
4,851

Hi All.

I want to create a structure during runtime. There are 3 fields A,B,C.

During Creation of Structure it MIGHT Have any combination of components A,B,C.

How do i create that structure.

For eg: Struct 1,

A Type ....

B Type ...

End of Struct 1.

Struct 2

B Type...

C Type..

End Struct 2.

Struct 3

B Type...

End Struct 2.

So the Dynamically structure can have any components ?

PLEASE HELP URGENTLY.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
3,496

I think if you want the structure to be dynamic, you need to build it on the fly, in this case I think you need to create a dynamic internal table first, and then create structure or work area based off that. Check this blog for the solution. Check the part where <dyn_wa> is created, this is a field symbol for a dynamically built structure.

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

Regards,

Rich Heilman

29 REPLIES 29
Read only

Former Member
0 Likes
3,496

This seems to be easy:

Have you ever heard of RTTI (Run Time Type Interface)?

To create a descriptor of a structure dynamically at run time, call the static method CREATE of class CL_ABAP_STRUCTDESCR and provide a list of the components to parameter P_COMPONENTS. The result is a descriptor of that structure.

Afterwards you may execute CREATE DATA ... TYPE HANDLE structure descriptor to create a data object having this structure.

Read only

Former Member
0 Likes
3,496

Hi,

use field-groups in stead of creating the structure.

REPORT demo_extract.

NODES: spfli, sflight.

FIELD-GROUPS: header, flight_info, flight_date.

START-OF-SELECTION.

INSERT: spfli-carrid spfli-connid

INTO header,

spfli-cityfrom spfli-cityto

INTO flight_info.

INSERT:sflight-fldate INTO header.

(after inserting the fields u want u can extract this will stop further insertion of the fields)

GET spfli. (instead u can use selection statements here)

EXTRACT flight_info.

(move the values in the fields to some internal table)

GET sflight.

EXTRACT flight_date.

(move the values in the fields to some internal table)

end-of-selection.

(u can print the data here)

rgds,

bharat.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
3,497

I think if you want the structure to be dynamic, you need to build it on the fly, in this case I think you need to create a dynamic internal table first, and then create structure or work area based off that. Check this blog for the solution. Check the part where <dyn_wa> is created, this is a field symbol for a dynamically built structure.

/people/rich.heilman2/blog/2005/07/27/dynamic-internal-tables-and-structures--abap

Regards,

Rich Heilman

Read only

0 Likes
3,496

Rich:

You don´t have to create an internal table first. You can create a structure descriptor using RTTI (CL_ABAP_STRUCTDESCR) and create an data object afterwards referencing this structure descriptor.

Read only

0 Likes
3,496

Here an example:


TYPE-POOLS:
  abap.

DATA:
  gr_structdescr    TYPE REF TO cl_abap_structdescr,
  gt_components     TYPE abap_component_tab,
  gw_component      TYPE LINE OF abap_component_tab,
  gr_wa             TYPE REF TO data.


START-OF-SELECTION.

* determine components of structure -> GT_COMPONENTS
  MOVE 'COMP1' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_string( ).
  INSERT gw_component INTO TABLE gt_components.

  MOVE 'COMP2' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_i( ).
  INSERT gw_component INTO TABLE gt_components.


* get structure descriptor -> GR_STRUCTDESCR
  gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).


* create work area of structure GR_STRUCTDESCR -> GR_WA
  CREATE DATA gr_wa TYPE HANDLE gr_structdescr.

<REMOVED BY MODERATOR>

Edited by: Alvaro Tejada Galindo on Feb 11, 2008 5:18 PM

Read only

0 Likes
3,496

Mike.

How do you describe fields/components as KEY using RTTI

Beacuase some components will act as a key in internal table.

Read only

0 Likes
3,496

Here the enhanced coding, that also creates an internal table:


TYPE-POOLS:
  abap.

DATA:
  gr_structdescr    TYPE REF TO cl_abap_structdescr,
  gr_tabledescr     TYPE REF TO cl_abap_tabledescr,
  gt_components     TYPE abap_component_tab,
  gw_component      TYPE LINE OF abap_component_tab,
  gt_keys           TYPE abap_keydescr_tab,
  gw_key            TYPE LINE OF abap_keydescr_tab,
  gr_wa             TYPE REF TO data,
  gr_itab           TYPE REF TO data.


FIELD-SYMBOLS:
  <gw_wa>           TYPE ANY,
  <gt_itab>         TYPE ANY TABLE.


START-OF-SELECTION.

* determine components of structure -> GT_COMPONENTS
  MOVE 'COMP1' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_string( ).
  INSERT gw_component INTO TABLE gt_components.

  MOVE 'COMP2' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_i( ).
  INSERT gw_component INTO TABLE gt_components.


* get structure descriptor -> GR_STRUCTDESCR
  gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).


* create work area of structure GR_STRUCTDESCR -> GR_WA
  CREATE DATA gr_wa TYPE HANDLE gr_structdescr.

  ASSIGN gr_wa->* TO <gw_wa>.


* determine key components -> GT_KEYS
  MOVE 'COMP1' TO gw_key-name.
  INSERT gw_key INTO TABLE gt_keys.


* create descriptor for internal table -> GR_TABLEDESCR
  gr_tabledescr ?= cl_abap_tabledescr=>create( p_line_type  = gr_structdescr
                                               p_table_kind = cl_abap_tabledescr=>tablekind_hashed
                                               p_unique     = abap_true
                                               p_key        = gt_keys
                                               p_key_kind   = cl_abap_tabledescr=>keydefkind_user ).


* create internal table -> GR_ITAB
  CREATE DATA gr_itab TYPE HANDLE gr_tabledescr.

  ASSIGN gr_itab->* TO <gt_itab>.

  sy-subrc = sy-subrc.

<REMOVED BY MODERATOR>

Edited by: Alvaro Tejada Galindo on Feb 11, 2008 5:19 PM

Read only

0 Likes
3,496

Very nice Mike. I like it. I did not know that you could create a structure by adding the components like that. Very nice. Thanks.

Regards,

Rich Heilman

Read only

0 Likes
3,496

Mike.

Thats a good one. Will assign points.

May i have your email address in case i need to contact you please Mike ?

Read only

0 Likes
3,496

@AMC:

Thanks. Have a look at my Profile here in SDN by clicking my name on the left side of this message, there you should find my eMail address, don´t you?!

--MIKE

Read only

Former Member
0 Likes
3,496

hi

good

check this code of creating dynamic structure and work accordingly,hope this would give you the solution of your problem.

REPORT zmaschl_create_data_dynamic .

TYPE-POOLS: slis.

DATA: it_fcat TYPE slis_t_fieldcat_alv,

is_fcat LIKE LINE OF it_fcat.

DATA: it_fieldcat TYPE lvc_t_fcat,

is_fieldcat LIKE LINE OF it_fieldcat.

DATA: new_table TYPE REF TO data.

DATA: new_line TYPE REF TO data.

FIELD-SYMBOLS: <l_table> TYPE ANY TABLE,

<l_line> TYPE ANY,

<l_field> TYPE ANY.

  • Build fieldcat

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = 'SYST'

CHANGING

ct_fieldcat = it_fcat[].

LOOP AT it_fcat INTO is_fcat WHERE NOT reptext_ddic IS initial.

MOVE-CORRESPONDING is_fcat TO is_fieldcat.

is_fieldcat-fieldname = is_fcat-fieldname.

is_fieldcat-ref_field = is_fcat-fieldname.

is_fieldcat-ref_table = is_fcat-ref_tabname.

APPEND is_fieldcat TO it_fieldcat.

ENDLOOP.

  • Create a new Table

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcat

IMPORTING

ep_table = new_table.

  • Create a new Line with the same structure of the table.

ASSIGN new_table->* TO <l_table>.

CREATE DATA new_line LIKE LINE OF <l_table>.

ASSIGN new_line->* TO <l_line>.

  • Test it...

DO 30 TIMES.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

<l_field> = sy-index.

INSERT <l_line> INTO TABLE <l_table>.

ENDDO.

LOOP AT <l_table> ASSIGNING <l_line>.

ASSIGN COMPONENT 'SUBRC' OF STRUCTURE <l_line> TO <l_field>.

WRITE <l_field>.

ENDLOOP.

thanks

mrutyun^

Read only

0 Likes
3,496

Rich

how can you create internal table first. The fields that structure will have as an component will come from selection screens where users will select by checking the checkbox that i want these fields in the internal table.

Read only

Former Member
0 Likes
3,496

Matt,

I have a function module that has dynamic generation of internal table code. The generation has succesfully.

How do i call the function module given the and get back a dynamic internal table given the above code in the function module

Please help

Read only

0 Likes
3,496

Could you please paste the code of your FM? That would make things easier

Read only

0 Likes
3,496

Guys,

I understood that part. Thats fine.

Another challenge that i am facing now is that i want to use RTTC/RTTI to dynamically create a internal table looking at th e structure of another internal table.

For eg: Table A is created dynamically by the system and then i want to create another Table B dynamically looking at the structure of Table A.

Please help. THis is very important to my work.

Read only

0 Likes
3,496

Hi AMC,

you can use the RTTI type descriptor (table type) more than just one time. So it should be no problem to create another internal table of the same type...

--MIKE

Read only

0 Likes
3,496

Do you have an example code please. The important thing to this is that the keys are different. Table A has 2 keys and Tale B has 3 keys

Please provide an code example

Read only

0 Likes
3,496

TYPE-POOLS:
  abap.
 
DATA:
  gr_structdescr    TYPE REF TO cl_abap_structdescr,
  gr_tabledescr1    TYPE REF TO cl_abap_tabledescr,
  gr_tabledescr2    TYPE REF TO cl_abap_tabledescr,
  gt_components     TYPE abap_component_tab,
  gw_component      TYPE LINE OF abap_component_tab,
  gt_keys           TYPE abap_keydescr_tab,
  gw_key            TYPE LINE OF abap_keydescr_tab,
  gr_wa             TYPE REF TO data,
  gr_itab1          TYPE REF TO data,
  gr_itab2			TYPE REF TO data.
 
 
FIELD-SYMBOLS:
  <gw_wa>           TYPE ANY,
  <gt_itab1>        TYPE ANY TABLE,
  <gt_itab2>		TYPE ANY TABLE.
 
 
START-OF-SELECTION.
 
* determine components of structure -> GT_COMPONENTS
  MOVE 'COMP1' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_string( ).
  INSERT gw_component INTO TABLE gt_components.
 
  MOVE 'COMP2' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_i( ).
  INSERT gw_component INTO TABLE gt_components.
 
 
* get structure descriptor -> GR_STRUCTDESCR
  gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).
 
 
* create work area of structure GR_STRUCTDESCR -> GR_WA
  CREATE DATA gr_wa TYPE HANDLE gr_structdescr.
 
  ASSIGN gr_wa->* TO <gw_wa>.
 
 
* determine key components -> GT_KEYS
  MOVE 'COMP1' TO gw_key-name.
  INSERT gw_key INTO TABLE gt_keys.
 
 
* create descriptor for internal table -> GR_TABLEDESCR1
  gr_tabledescr1 ?= cl_abap_tabledescr=>create( p_line_type  = gr_structdescr
                                               p_table_kind = cl_abap_tabledescr=>tablekind_hashed
                                               p_unique     = abap_true
                                               p_key        = gt_keys
                                               p_key_kind   = cl_abap_tabledescr=>keydefkind_user ).
 
 
* create internal table -> GR_ITAB1
  CREATE DATA gr_itab1 TYPE HANDLE gr_tabledescr1.
 
  ASSIGN gr_itab1->* TO <gt_itab1>.
  
  
* determine key components -> GT_KEYS
  MOVE 'COMP2' TO gw_key-name.
  INSERT gw_key INTO TABLE gt_keys.
 
 
* create descriptor for internal table -> GR_TABLEDESCR2
  gr_tabledescr2 ?= cl_abap_tabledescr=>create( p_line_type  = gr_structdescr
                                               p_table_kind = cl_abap_tabledescr=>tablekind_hashed
                                               p_unique     = abap_true
                                               p_key        = gt_keys
                                               p_key_kind   = cl_abap_tabledescr=>keydefkind_user ).
 
 
* create internal table -> GR_ITAB2
  CREATE DATA gr_itab2 TYPE HANDLE gr_tabledescr2.
 
  ASSIGN gr_itab2->* TO <gt_itab2>.
  
  
  
* <GT_ITAB1> has a key with 1 component
* <GT_ITAB2> has a key with 2 components
Read only

0 Likes
3,496

Many Thanks.

My problem is that i am not creating the structure from the scratch but i have the internal table of type hashed and i want to use that table to modify the keys of the same hashed table or the new hashed table.

Please help with code sample i am appreciating your every second here.

Read only

0 Likes
3,496

Many Thanks.

My problem is that i am not creating the structure from the scratch but i have the internal table of type hashed and i want to use that table to modify the keys of the same hashed table or the new hashed table.

For Eg: Table 1 has 2 keys ( Table Generated By the system assigned to field symbo <lt_down>)

I want to create another table of same kind but specify delete this key and specify 1 more in a new table <lt_down2>

Please help with code sample i am appreciating your every second here.

Read only

0 Likes
3,496

Hi

I have the follwoing code:

 

 gr_tabledescr ?= cl_abap_typedescr=>describe_by_data( I_temptable ).
ASSIGN gr_tabledescr->KEY TO <tab_keys>.

DATA: s_temp type REF TO data.
CREATE DATA s_temp LIKE LINE OF <tab_keys>.
FIELD-SYMBOLS: <s_Line> type any,
               <s_line_struct> type any.
ASSIGN s_temp->* TO <S_LINE>.

loop at <tab_keys> into <s_line>.

   if <s_line> = 'ZFD'.
     
       <s_line> = 'ZCS'.
    
  endloop.

modify <tab_keys> from <s_line>.

Now, why does this code throws an exception when i try to modify the keys of this table. One of the key is ZFD, i want to replace with 'ZCS'.

It say the fieldsymbols are protected exception.

Please help.

Edited by: AMC on Feb 12, 2008 11:01 AM

Read only

0 Likes
3,496

I would do the following:

1.) Get an table type descriptor for the table type stored in DDIC (hashed table)

2.) Afterwards, create a new structure similar to the structure of the DDIC-table type

3.) Then, create a new table type descriptor asccording to the table type descriptor mentionen in 1.), but when defining the key components, restrict the key to the components fullfilling your needs.

That should be it. Please apologize, but i cannot post code for all of your problems. A little bit of work should be done by yourself, too!

Read only

0 Likes
3,496

Hi.

I did that.

I want to change the datatype of the components of structure as well ?

Please help. Can you provide a small snippet in this case ?

Read only

0 Likes
3,496

Sorry, but wouldn´t it be better to generate a descriptor from scratch?! To me, it seems so ...

Read only

0 Likes
3,496

Thing is that, The system generated table has components of a particular data type.

That table is of type hashed. I want to create another table by looking at this table modifying the keys plus data type.

THe reason for modificatin of the data type is that when you perform a collect on hashed table the components that are not keys has to be of type C, I, P,N other wise it throws exception and thats why i am modifying the data type ?

Read only

0 Likes
3,496

You have to loop through the components of the DDIC-defined table and while looping you have to fill an internal table containing the components of the other internal table you want to create. Before INSERTing the components to the component-table you have to modify the type of teh according component. And then you´re done. Use the new created component table to generate a structure descriptor. This structure descriptor should then in turn be used to create a descriptor for an internal table.

Read only

0 Likes
3,496

Great Message. Thats fine now.

I want to use ALV Display simple one to display the output. Have you got any sample code for Dynamically generated tables? Can you paste it here ?

Read only

0 Likes
3,496

Please apologize, but i cannot post code for all of your problems. A little bit of work should be done by yourself, too!

I thank, there are enough sample codings showing you how to use ALV...

Read only

0 Likes
3,496

Thanks for your help.

I will assign points.