Application Development 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: 

Creating tables dynamically in loop

mohammadaamir_khan
Participant
0 Kudos
726

Hi,

I have an internal table ( say table_1) in which 1 field ( KOTABNR ) holds the table name.

I need to loop at table_1 and have to create as many internal tables as there are entries in the field KOTABNR.

I am not able to declare field symbol names dynamically so that I can identify which table is created against a particular entry in table_1.

Please help on the issue.

Thanks

1 ACCEPTED SOLUTION

matt
Active Contributor
645

Try something like

TYPES: BEGIN OF ty_table,
         tabname TYPE tabname,
         contents TYPE REF TO DATA,
       END OF ty_table.
TYPES ty_tables TYPE HASHED TABLE OF ty_table.
DATA tables TYPE ty_tables.
LOOP AT kobog_temp INTO kobog.
  DATA(tablename) = 'A' && kobog-kotabnr.
  INSERT VALUE #( tabname = tablename ) INTO TABLE tables
       ASSIGNING FIELD-SYMBOL(<table>).
  CREATE DATA <table>-contents TYPE TABLE OF (tablename).
  FIELD-SYMBOLS <contents> TYPE STANDARD TABLE.
  ASSIGN <table>-contents->* to <contents>.
  SELECT * INTO TABLE <contents> FROM (tablename)
       FOR ALL ENTRIES IN konaind
       WHERE knumh = koonaind-knumh.
ENDLOOP.

Then, when you need to read one of the tables, something like

FIELD-SYMBOLS <contents> TYPE STANDARD TABLE.
ASSIGN tables[ tabname = tablename ]-contents-> TO <contents>.
4 REPLIES 4

Sandra_Rossi
Active Contributor
0 Kudos
645

The declaration of a field symbol is somewhat "dynamic":

FIELD-SYMBOLS <ITAB> TYPE ANY TABLE.

Each time you need to access one of the many internal tables, you do:

ASSIGN dref_itab->* TO <itab>.

Post your code if you're still not clear with the concept.

mohammadaamir_khan
Participant
0 Kudos
645

I am adding code snippet. Table lt_kobog_temp has a field KOTABNR which has values for condition tables like 901, 902.....and we have to prepare internal table dynamically like it_a901, it_902..so that after loop, i can read each table in sequence .

In the below code, only one internal table <f_itab> gets created. If somehow, I can declare field symbol name dynamically inside loop, then i can create many internal tables dynamically but right now, i am unable to achieve it.

FIELD-SYMBOLS : <f_itab> TYPE ANY TABLE.
DATA : w_dref TYPE REF TO data,

lv_table TYPE tabname.

* Now preparing the table
LOOP AT lt_kobog_temp INTO lwa_kobog.

CONCATENATE 'A' lwa_kobog-KOTABNR INTO lv_table.
CREATE DATA w_dref TYPE TABLE OF (lv_table).
ASSIGN w_dref->* TO <f_itab>.

SELECT * INTO TABLE <f_itab>
FROM (lv_table) FOR ALL ENTRIES IN lt_konaind
WHERE knumh = lt_konaind-knumh.

ENDLOOP.

matt
Active Contributor
646

Try something like

TYPES: BEGIN OF ty_table,
         tabname TYPE tabname,
         contents TYPE REF TO DATA,
       END OF ty_table.
TYPES ty_tables TYPE HASHED TABLE OF ty_table.
DATA tables TYPE ty_tables.
LOOP AT kobog_temp INTO kobog.
  DATA(tablename) = 'A' && kobog-kotabnr.
  INSERT VALUE #( tabname = tablename ) INTO TABLE tables
       ASSIGNING FIELD-SYMBOL(<table>).
  CREATE DATA <table>-contents TYPE TABLE OF (tablename).
  FIELD-SYMBOLS <contents> TYPE STANDARD TABLE.
  ASSIGN <table>-contents->* to <contents>.
  SELECT * INTO TABLE <contents> FROM (tablename)
       FOR ALL ENTRIES IN konaind
       WHERE knumh = koonaind-knumh.
ENDLOOP.

Then, when you need to read one of the tables, something like

FIELD-SYMBOLS <contents> TYPE STANDARD TABLE.
ASSIGN tables[ tabname = tablename ]-contents-> TO <contents>.

0 Kudos
645

Thanks Matthew