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

AT Statement in Nested Loops

former_member186413
Participant
0 Likes
3,235

Hi Expert,

Something that I have found out which is quite interesting. Which is about AT statement in the nested loops. Take the following code as the sample.

TABLES: VBUK,

               VBUP.

DATA: itab_vbuk TYPE TABLE OF VBUK WITH HEADER LINE,

           itab_vbup TYPE TABLE OF VBUP WITH HEADER LINE.

DATA: counter TYPE i.

START-OF-SELCTION.

SELECT * FROM VBUK INTO TABLE  itab_vbuk.

SELECT * FROM VBUP INTO TABLE  itab_vbup.

LOOP AT itab_vbuk.

     LOOP AT itab_vbup WHERE VBELN = itab_vbuk-vbeln

         IF itab_vbup-wbsta = 'C' .

               counter = counter + 1.

         ENDIF.

        AT LAST.

            WRITE counter.

          CLEAR counter.

         ENDAT.

     ENDLOOP.

ENDLOOP.

From the sence of logic of this program AT LAST which is correponding to the inner loop, but what I found out is that 'AT LAST' is respond to the outer loop. So are there anyone else have similar case as mine, how would you do if inner loop was about to be the last?

1 ACCEPTED SOLUTION
Read only

arindam_m
Active Contributor
0 Likes
2,445

Hi,

The AT LAST control level is always at the end of the ITAB loop in which it is invoked. The filter (where condition) that you apply is not applicable to the control break key word AT LAST as it refers the whole table instead of the filtered values and the event would be triggered after the last entry of the table instead of the last entry of the filtered values as you are expecting.

And below is the debug screenshot. As you can see the write inside AT LAST is executed after the last entry of itab_vbup as indicated by SY-TABIX.

So what is happening is the moment when a vbeln value from ITAB_VBUK is equal to the last vbeln value in ITAB_VBUP the AT LAST event is triggered as you reach the last entry of TAB_VBUP (by using your where condition) The index will make things clear. The event is still triggered by the LOOP AT ITAB_VBUP but just that the where clause has made the understanding tricky.

Refer the link:

http://help.sap.com/saphelp_470/helpdata/en/fc/eb381a358411d1829f0000e829fbfe/content.htm

Cheers,

Arindam

8 REPLIES 8
Read only

Former Member
0 Likes
2,445

hello,

Don't use the control statement in loop statements.(i.e. do not sue where clause in the inside loop statement)

best regards

swanand

Read only

Former Member
0 Likes
2,445

Hi Hai,

I tested your code and 'At Last; is working for inner loop only.

Screenshots -

in SE16 -

and program output ->

So, please re try the code at your end.

BR.

Read only

arindam_m
Active Contributor
0 Likes
2,446

Hi,

The AT LAST control level is always at the end of the ITAB loop in which it is invoked. The filter (where condition) that you apply is not applicable to the control break key word AT LAST as it refers the whole table instead of the filtered values and the event would be triggered after the last entry of the table instead of the last entry of the filtered values as you are expecting.

And below is the debug screenshot. As you can see the write inside AT LAST is executed after the last entry of itab_vbup as indicated by SY-TABIX.

So what is happening is the moment when a vbeln value from ITAB_VBUK is equal to the last vbeln value in ITAB_VBUP the AT LAST event is triggered as you reach the last entry of TAB_VBUP (by using your where condition) The index will make things clear. The event is still triggered by the LOOP AT ITAB_VBUP but just that the where clause has made the understanding tricky.

Refer the link:

http://help.sap.com/saphelp_470/helpdata/en/fc/eb381a358411d1829f0000e829fbfe/content.htm

Cheers,

Arindam

Read only

0 Likes
2,445

Ya, There are somethings is much more tricky is that, when at place AT LAST with AT END OF vbeln, the AT statement will be exceute everytime inside the inner loop.

Read only

arindam_m
Active Contributor
0 Likes
2,445

Hi,

That's because the inner loop then has a new VBELN everytime it enters the loop the event of AT END OF is triggered at the end of VBELN and as you have not sorted so chances are there ar no adjoining VBELN in the outer loop table.

Cheers,

Arindam

Read only

0 Likes
2,445

As the metter of fact, Outter loop table hasn't been sorted yet, but the inner loop table does.

Read only

Former Member
0 Likes
2,445

Hi,

The other alternative is that you loop at VBUP as you need to put 'AT LAST' statement for this loop and read the table for VBUK as below:

DATA: itab_vbuk TYPE STANDARD TABLE OF vbuk ,

          wa_vbuk   TYPE vbuk,

          itab_vbup TYPE  STANDARD TABLE OF VBUP,

          wa_vbup TYPE vbup.

DATA: counter TYPE i.

START-OF-SELCTION.

SELECT * FROM VBUK INTO TABLE  itab_vbuk.

if sy-subrc eq 0.

sort itab_vbuk STABLE BY vbeln.

endif.

SELECT * FROM VBUP INTO TABLE  itab_vbup.

if sy-subrc eq 0.

sort itab_vbup STABLE BY vbeln posnr.

endif.

LOOP AT itab_vbup into wa_vbup where wbsta = 'C'.

     READ TABLE itab_vbuk into wa_vbuk WHERE VBELN = itab_vbup-vbeln

                                                                         BINARY SEARCH.

     IF SY-SUBRC EQ 0.

         counter = counter + 1.

     ENDIF.

     AT END OF vbeln.

          WRITE counter.

          CLEAR counter.

     ENDAT.

CLEAR : WA_VBUP, WA_VBUK.

ENDLOOP.

Let me know if you need any more details.

Read only

Former Member
0 Likes
2,445

at event will not work properly for loop at itab with condition.

It will work for loop at itab without any condition.

You can add this logic to your program..

TABLES: VBUK, VBUP.
DATA: itab_vbuk TYPE TABLE OF VBUK WITH HEADER LINE,
       itab_vbup TYPE TABLE OF VBUP,
       itab_vbup1 TYPE TABLE OF VBUP,
       wa_vbup1 like line of itab_vbup1,
       wa_vbeln(10) type c.
DATA: counter TYPE i.
START-OF-SELECTION.
   SELECT * FROM VBUK INTO TABLE  itab_vbuk .
   SELECT * FROM VBUP INTO TABLE  itab_vbup .
 
   sort itab_vbuk by vbeln.
   sort itab_vbup by vbeln.

   LOOP AT itab_vbuk.
     clear itab_vbup1.
     itab_vbup1[] = itab_vbup[].
     delete itab_vbup1 where vbeln ne itab_vbuk-vbeln.
     wa_vbeln = itab_vbuk-vbeln.
     LOOP AT itab_vbup1 into wa_vbup1.
       IF wa_vbup1-wbsta = 'C' .
         counter = counter + 1.
       ENDIF.
       AT LAST.
         WRITE😕 wa_vbeln, counter.
         CLEAR counter.
       ENDAT.
     ENDLOOP.

   ENDLOOP.