‎2011 Jul 21 12:30 PM
Hi Everyone,
I realise there are plenty of threads on this topic but I cant find one specific to my scenario. The examples I've found are for one dynamic table or for multiple dynamic tables where the number of tables required is known. Maybe I'm overlooking something in the examples I have seen but I hope not.
My scenario is that I need to create multiple internal tables all of the same structure depending on the number of records that are in a table . Eg. itab_1 contains 10 records so I need 10 internal tables to be created dynamically. This could be any amount so I dont want to code like <fs_itab_1>, <fs_itab_2> etc becuase I could end up declaring very many of these.
Is there any way I can create tables dynamically depending on the number of records in a table without having to declare a field symbol for each one I want ?
I would appreciate any help or pointers on this ?
Thanks so much for you time and consideration.
Kind Regards
DK
Edited by: Damien Kesle on Jul 21, 2011 1:36 PM
‎2011 Jul 21 2:41 PM
Hi,
Looks like quite interesting requirement.
But, what do you do with those may be X number of internal tables? How do you process them afterwards? You should think of these questions before you create those dynamic internal tables.
Anyways, I look at the requirement as one internal table(Complex type) which actually holds the internal tables. I will add those number of internal tables as rows to this complex internal tables. I mean use the definition of your dynamic internal table as a row of another internal table which holds multiple number of internal tables(as decided at the runtime)
Like if you are looking at <fs_int_1> as one internal table, I will say you have access the first row of complex internal table.
Hope this satisfies your requirement.
Regards,
Ravi
‎2011 Jul 21 2:41 PM
Hi,
Looks like quite interesting requirement.
But, what do you do with those may be X number of internal tables? How do you process them afterwards? You should think of these questions before you create those dynamic internal tables.
Anyways, I look at the requirement as one internal table(Complex type) which actually holds the internal tables. I will add those number of internal tables as rows to this complex internal tables. I mean use the definition of your dynamic internal table as a row of another internal table which holds multiple number of internal tables(as decided at the runtime)
Like if you are looking at <fs_int_1> as one internal table, I will say you have access the first row of complex internal table.
Hope this satisfies your requirement.
Regards,
Ravi
‎2011 Jul 21 2:45 PM
I think dynamically declaring the variables is not quite possible.
AnyhowInteresting question and intersting Approach from RAVI.
‎2011 Jul 21 3:09 PM
Ravi explained how semantically this should more or less work, now the code;)
DATA it_dyn_tables TYPE TABLE OF REF TO data.
DATA wa_dyn_table TYPE REF TO data.
DO 10 TIMES. "any times you want
CREATE DATA wa_dyn_table TYPE TABLE OF sflight. "create dynamic table
APPEND wa_dyn_table TO it_dyn_tables. "and store it in table of references
ENDDO.
FIELD-SYMBOLS <dyn_table> TYPE ANY TABLE.
LOOP AT it_dyn_tables INTO wa_dyn_table.
ASSIGN wa_dyn_table->* TO <dyn_table>.
"now do whatever you want with <dyn_table> which is one of your dynamic tables
ENDLOOP.I used table of references as they can accept generic data objects. This can be either reference to data object, structure, or (as in this case) table.
Regards
Marcin
‎2011 Jul 25 11:36 AM
Hi Guys,
Thank to you all for your responses. Apologies for my late reply as I was out for the last few days.
I have coded a soluton by basically declaring 50 interntal tables (gt_tab) and assiging the internal table name plus the counter (gt_tab1) to the field-symbols. I'm interested in changing this after seeing your suggestion RAVI. Thanks alot for the idea.
SSM - Yes, my initial approach was not correct for the way I had consider to do it. You do need to declare at least the table name this way and assign to field symbol as I mentioned above.
Marcin - Thanks for the coding to illustrate RAVI's suggestion.
Points Awarded - Thanks again for all your help guys.
Regards
DK