Skip to Content
avatar image
Former Member

Regarding LOOP Statement in ABAP

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.

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

5 Answers

  • Best Answer
    avatar image
    Former Member
    May 16, 2006 at 03:42 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • May 16, 2006 at 03:26 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • May 16, 2006 at 03:37 AM

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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 16, 2006 at 03:26 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • May 16, 2006 at 03:26 AM

    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!

    Add comment
    10|10000 characters needed characters exceeded