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 internal table name

Former Member
0 Likes
1,248

Hello,

Can anyone tell me how can I use a dynamic table name:

Something like this:

data my_table(20).

my_table = 'MARA'.

loop at (my_table).

stuff...

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
592

You can do this using field symbols.

Regards,

Rich Heilman

3 REPLIES 3
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
593

You can do this using field symbols.

Regards,

Rich Heilman

Read only

0 Likes
592

Something like this.



report zrich_0001 .

data: itab1 type table of string with header line.
data: itab2 type table of string with header line.

field-symbols: <fs_tab> type table,
               <fs_wa>.


itab1 = 'dataa1'.   append itab1.
itab1 = 'dataa2'.   append itab1.
itab1 = 'dataa3'.   append itab1.

itab2 = 'datab1'.   append itab2.
itab2 = 'datab2'.   append itab2.
itab2 = 'datab3'.   append itab2.


assign itab1[] to <fs_tab>.
assign itab1 to <fs_wa>.

loop at <fs_tab> into <fs_wa>.
  write:/ <fs_wa>.
endloop.



assign itab2[] to <fs_tab>.
assign itab2 to <fs_wa>.

loop at <fs_tab> into <fs_wa>.
  write:/ <fs_wa>.
endloop.

Regards,

Rich Heilman

Read only

0 Likes
592

If you need to reference the actual fields of the internal table, then you can do it like this.



report zrich_0001 .

types: begin of ttab,
       fld1(10) type c,
       fld2(10) type c,
       fld3(10) type c,
       end of ttab.

data: itab1 type table of ttab with header line.
data: itab2 type table of ttab with header line.

field-symbols: <fs_tab> type table,
               <fs_wa>,
               <fs>.


itab1-fld1 = 'dataa1'.
itab1-fld2 = 'dataa2'.
itab1-fld3 = 'dataa3'.
append itab1.

itab2-fld1 = 'datab1'.
itab2-fld2 = 'datab2'.
itab2-fld3 = 'datab3'.
append itab2.


assign itab1[] to <fs_tab>.
assign itab1 to <fs_wa>.

loop at <fs_tab> into <fs_wa>.
  do.
    assign component sy-index of structure <fs_wa> to <fs>.
    if sy-subrc <> 0.
      exit.
    endif.
    if sy-index = 1.
      write:/ <fs>.
    else.
      write: <fs>.
    endif.
  enddo.
endloop.



assign itab2[] to <fs_tab>.
assign itab2 to <fs_wa>.

loop at <fs_tab> into <fs_wa>.
  do.
    assign component sy-index of structure <fs_wa> to <fs>.
    if sy-subrc <> 0.
      exit.
    endif.
    if sy-index = 1.
      write:/ <fs>.
    else.
      write: <fs>.
    endif.
  enddo.
endloop.

Regards,

Rich Heilman