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

Regarding LOOP Statement in ABAP

Former Member
0 Likes
8,625

Hi,

I have small question regarding <b>LOOP ..... ENDLOOP</b> statement against <b>SY-SUBRC</b> Check.

I have a loop as below:

<b> LOOP AT i_vbap WHERE vbeln = i_vbak-vbeln.

  • Some Code

ENDLOOP.

IF sy-subrc <> 0.

APPEND i_sdata.

CLEAR i_sdata.

ENDIF.</b>

Is the above <b>Code/Syntax</b> is correct one.

Can we use <b>SY-SUBRC</b> Check against <b>LOOP ... ENDLOOP</b> statement.

Is this the right way of writing the code as per standards.

Can anybody give sujjestions regarding the same.

Thanks in advance.

Thanks & Regards,

Prasad.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,849

Hi Prasad,

The stetements are absolutely right.

what it do is ,

to Check the table I_VBAP has any records for each I_VBAK-VBELN,if so appending the record to I_SDATA. else not doing any thing.

SY-SUBRC Will be 0 only when each VBAK-VBELN, have atleast 1 record in VBAP table. then only it will become 0.

Regards

Srikanth

5 REPLIES 5
Read only

Former Member
0 Likes
2,849

Hi Prasad,

Syntax is correct only, here one suggestion is that in this case you can use READ instead of LOOP ENDLOOP unless if you are not doing anything with the internal table data.

Regards,

Arun.

Read only

aris_hidalgo
Contributor
0 Likes
2,849

Hi Prasad,

Yes, you could use sy-subrc after the endloop. For example:

loop at itab1 where kunnr = itab2-kunnr.

some conditions...

endloop.

if sy-subrc = 0.

write: 'Success!'.

else.

leave program.

endif.

if you press F1 while highlighting the LOOP statement here are the meaning of sy-subrc in loop...endloop.

SY-SUBRC = 0:

At least one loop pass was processed.

SY-SUBRC = 4:

The loop was not processed because the table contains no entries or no entries satisfied the conditions.

Regards!

Read only

Simha_
Product and Topic Expert
Product and Topic Expert
0 Likes
2,849

Hi Prasad,

U can use sy-subrc against Loop .. Endloop.

It's normal programming convention u r checking.

It's doesn;t lead to any programming errors.

Even u can try it in ur logic.

Cheers,

Simha.

Read only

former_member186741
Active Contributor
0 Likes
2,849

yes, this is the correct way to check sy-subrc.

As the sap help says,

"At the end of loop processing (that is after ENDLOOP), the return code value of SY-SUBRC specifies whether the loop was actually processed.

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."

Read only

Former Member
0 Likes
2,850

Hi Prasad,

The stetements are absolutely right.

what it do is ,

to Check the table I_VBAP has any records for each I_VBAK-VBELN,if so appending the record to I_SDATA. else not doing any thing.

SY-SUBRC Will be 0 only when each VBAK-VBELN, have atleast 1 record in VBAP table. then only it will become 0.

Regards

Srikanth