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: 

loop at <itab>

Former Member
0 Kudos

is the sy-subrc set inside a loop?

i had a scenario in which the internal table header line contained a record that was selected after certain conditions were satisfied.

i had a checked if sy-subrc equal to 0 and then proceeded. There were 3 records which satisfied the conditions; of which the first 2 were correctly populated but the 3rd record was rejected since the sy-subrc was not eq 0.

the syntax was like:

loop at <itab> were <conditions>.

if sy-subrc eq 0.

..

..

endif.

How is that the sy-subrc became eq to 0 within the loop?

Message was edited by:

John George

1 ACCEPTED SOLUTION

amit_khare
Active Contributor
0 Kudos

Hi John,

Welcome to SDN.

Is it necessary to use Loop - Endloop?

If so, then instead of checking for sy-subrc you may put an If condition there for the checking of records.

Otherwise as an alternative use READ itab command and then you may able perform sy-subrc check too.

Regards,

Amit

Reward all helpful replies.

8 REPLIES 8

amit_khare
Active Contributor
0 Kudos

Hi John,

Welcome to SDN.

Is it necessary to use Loop - Endloop?

If so, then instead of checking for sy-subrc you may put an If condition there for the checking of records.

Otherwise as an alternative use READ itab command and then you may able perform sy-subrc check too.

Regards,

Amit

Reward all helpful replies.

0 Kudos

hi Amit,

yes i can use the READ syntax, but i wanted to know whether it is wise checking the sy-subrc condition inside a loop.

because the scenario i talked abt had a record in the header line which was satisfying all the conditions used in the where clause even though the sy-subrc was eq to 4.

Also these records are mapped from one database table to another. As in the records are first checked for in another table and for those records a corresponding record should exist in the other table. So would an incorrect mapping create such a scenario?

consider the case where the first table has 1 entry and the mapped table has 2 entries, the 2nd one created manually with simillar values. when you loop the first table and then loop on to the second table, both the records would be picked. But would the 2nd record set sy-subrc eq 4.

PS the loop of the second table does not exit.

0 Kudos

Hi John,

Maybe this helps, here's an excerpt of the SAP help on the LOOP AT statement:

<i>The LOOP AT statement sets system field sy-tabix to the table index of the current table line for Standard tables and sorted Tables, and to 0 for Hashed-Tables, after every loop pass. <b>It leaves sy-subrc unchanged</b>. When the loop is exited with ENDLOOP, sy-tabix is reset to the value it had before the loop was entered, and the following applies to sy-subrc:

- 0 if the loop was run at least once

- 4 if the loop was not run at all

</i>

I wrote a little test program to demonstrate:

types: begin of GT_NUM,

VALUE type I,

end of GT_NUM.

data: G_T_NUM type table of GT_NUM.

data: G_S_NUM type GT_NUM.

G_S_NUM-VALUE = 1.

append G_S_NUM to G_T_NUM.

G_S_NUM-VALUE = 2.

append G_S_NUM to G_T_NUM.

G_S_NUM-VALUE = 3.

append G_S_NUM to G_T_NUM.

sy-subrc = 8.

loop at G_T_NUM into g_s_num where VALUE <= 2.

write / 'in loop'.

write / sy-subrc.

endloop.

write / 'after loop'.

write / sy-subrc.

<u>Here's the result of the program:</u>

Program ZZ_LOOP_TEST

in loop

8

in loop

8

after loop

0

<u>Conclusion</u>

As mentioned in the help, SY-SUBRC inside the loop is meaningless; it is actually dangerous to use it at all since it will be exactly what it was <b>before</b> the loop is entered and can therefore lead to wrong results (if its use is intended to check on the status of the loop)!

Hope this helps!

Guenther

Former Member
0 Kudos

Hi George,

The following SY-SUBRC are set for Loop processing.

SY-SUBRC = 0:

The loop was executed at least once.

SY-SUBRC = 4:

The loop was not executed, either because there was no entry at all or because there was no entry which satisfied the conditions.

Trust this helps. Pls. reward points if helpful.

Regards,

Biju

0 Kudos

Hi Biju,

yes, i am aware of the way sy-subrc is assigned as you mentioned. but in this case, if sy-subrc eq 0, the statements within the loop will not be executed. But if you use an explicit if sy-subrc check the statements under loop ie. the if else part is executed.

dont understand how this is possible when the header line contains a record.

Former Member
0 Kudos

Hi John,

Welcome to SDN........

The statements inside LOOP....ENDLOOP with WHERE condition will execute only when the conditions specified in the WHERE cluase are met, So there no need to check for sy-subrc condition inside the LOOP...ENDLOOP.

Generally the sy-subrc values for LOOP...ENDLOOP with WHERE condition are as follows:

SY-SUBRC = 0 -> The loop was executed at least once.

SY-SUBRC = 4 -> The loop was not executed, either because there was no entry at all or because there was no entry which satisfied the conditions.

To fulfill your requirement just comment sy-subrc check in your code.

loop at <itab> were <conditions>.

<b>*if sy-subrc eq 0.</b>

..

..

<b>*endif.</b>

endloop.

Thanks,

Vinay

Former Member
0 Kudos

Hi John,

Welcome to SDN........

The statements inside LOOP....ENDLOOP with WHERE condition will execute only when the conditions specified in the WHERE cluase are met, So there no need to check for sy-subrc condition inside the LOOP...ENDLOOP.

Generally the sy-subrc values for LOOP...ENDLOOP with WHERE condition are as follows:

SY-SUBRC = 0 -> The loop was executed at least once.

SY-SUBRC = 4 -> The loop was not executed, either because there was no entry at all or because there was no entry which satisfied the conditions.

To fulfill your requirement just comment sy-subrc check in your code.

loop at <itab> were <conditions>.

<b>*if sy-subrc eq 0.</b>

..

..

<b>*endif.</b>

endloop.

Thanks,

Vinay

Former Member
0 Kudos

Hi

Check out the following code even if you put sy-subrc = 0 or not it is working fine.

Comment this sy-subrc then also it is working fine.

loop at it_lfa1 where land1 = 'AU'.

if sy-subrc = 0.

write:/ it_lfa1-lifnr, it_lfa1-name1, it_lfa1-land1.

endif.

endloop.

Regards

Haritha.