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

sy-index / sy-tabix wrong within loop

0 Likes
4,185

Hi friends,

i do a loop over a a table and am writing out the field contents like this:

 LOOP AT <dyn_table> INTO <dyn_wa>.

        do.

      assign component sy-index
         of structure <dyn_wa> to <dyn_field>.
        if sy-subrc <> 0.
         EXIT.
        else.
* Here it gets the name of the field based on the sy-index of the component

        READ TABLE l_tab_fields INTO w_tab_fields INDEX sy-index.
      endif.

  ENDLOOP.

What now doesnt work is, that whenever i have the read statement uncommented the loop doesnt increment so i only get the first row but that n-times.

any idea where the error is ?

thank you! i am awarding points generously

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,588

is ur loop statement correct??

If i am not wrong shouldnt it be like this

<b>LOOP AT <dyn_table> ASSIGNING <dyn_wa>.</b>

7 REPLIES 7
Read only

Former Member
0 Likes
2,588

Hi

for the DO

ENDDO is missing.

Write it and see

LOOP AT <dyn_table> INTO <dyn_wa>.

do.

assign component sy-index

of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

EXIT.

else.

  • Here it gets the name of the field based on the sy-index of the component

READ TABLE l_tab_fields INTO w_tab_fields INDEX sy-index.

endif.

ENDDO.

ENDLOOP.

reward if useful

regards,

ANJI

Read only

Former Member
0 Likes
2,589

is ur loop statement correct??

If i am not wrong shouldnt it be like this

<b>LOOP AT <dyn_table> ASSIGNING <dyn_wa>.</b>

Read only

Former Member
0 Likes
2,588

You have to use SY-TABIX in LOOPS...

and in DO etc you have to use SY-INDEX

That's the only problem....

Tabix as you are looping at an INTERNAL TABLE data...

Read only

Former Member
0 Likes
2,588

LOOP AT <dyn_table> INTO <dyn_wa>.

<b>lv_index = sy-tabix.</b>

do.

assign component sy-index

of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

EXIT.

else.

  • Here it gets the name of the field based on the sy-index of the component

READ TABLE l_tab_fields INTO w_tab_fields INDEX<b> lv_index.</b>

endif.

ENDLOOP.

Read only

0 Likes
2,588

hi, thank you for your immediate help. i debugged it once more and here is what i found:

The problem was that with the read statement inside the loop, the sy-tabix that is used by the loop itself was overwritten with the new value ( and that is 1 every time).

Read only

Former Member
0 Likes
2,588

Hi

Your code seems to be right, if LOOP doesn't increment the index it means there's only one record (?): let's know how many and which records are in DYN_TABLE and l_tab_fields.

Anyway you should use a variable to store the index of the first table:



DATA: TABIX TYPE SY-TABIX.

LOOP AT <dyn_table> INTO <dyn_wa>.
    
   TABIX = SY-TABIX.

   DO.
     assign component sy-index of structure <dyn_wa> to <dyn_field>.
     if sy-subrc <> 0.
       EXIT.
     else.
* Here it gets the name of the field based on the sy-index of the component
        READ TABLE l_tab_fields INTO w_tab_fields INDEX sy-index.

*---> Here if sy-subrc = 0, the SY-TABIX is equal to SY-INDEX.
      endif.
   ENDDO.

ENDLOOP.

Max

Read only

Former Member
0 Likes
2,588

LOOP AT <dyn_table> INTO <dyn_wa>.

do.

assign component sy-index

of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

EXIT.

else.

  • Here it gets the name of the field based on the sy-index of the component

READ TABLE l_tab_fields INTO w_tab_fields INDEX sy-index.

endif.

ENDLOOP.

If i gt you rite..i would suggest this

Data: l_tabix type sy-tabix.

LOOP AT <dyn_table> INTO <dyn_wa>.

l_tabix - sy-tabix.

assign component l_tabix

of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

EXIT.

else.

  • Here it gets the name of the field based on the sy-index of the component

READ TABLE l_tab_fields INTO w_tab_fields INDEX l_tabix.

endif.

ENDLOOP.

santhosh