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

Problem with AT END OF

Former Member
0 Likes
1,549

Hi there,

I'm having a weird problem with the END OF option of the AT statement. I already knew that it can get tricky because it compares not only the specified field's content after AT END OF, but also all the fields to its left, in the structure.

But this still doesn't answer why am I getting very weird results here.

Basically, this is my scenario:

My internal table:

MATKL COLUMN B COLUMN C

140 xxxxxx yyyyyyy

140 cfsdd dasdsa

140 pdasd oasdas

150 dsadas dasdas

150 asdasd asdasd

.

.

170 xxxx xxxx

170 xxx xxxx

.

.

The code I'm having problems with is:

LOOP AT my_tab INTO my_struct WHERE date <= p_date.
    ....code
    ....code

    AT END OF matkl .
      CLEAR ls_t023t.
      READ TABLE gt_t023t INTO ls_t023t WITH KEY matkl = my_struct-matkl spras = sy-langu.
      ls_alv_rep-wgbez = ls_t023t-wgbez.
      APPEND ls_alv_rep TO gt_alv_rep.
      CLEAR ls_alv_rep.
    ENDAT.
  ENDLOOP.

The table is, of course, sorted by MATKL, and there are no fields left to MATKL.

For some reason, the code within the END OF is only being triggered for a small set of the MATKLs from my table. Of course, I'm taking into account the WHERE clause in the LOOP statement, but what I'm saying is that once the code enters the loop, I'd expect that each MATKL the loop processes, eventually enters the END OF code, after the last line for the given MATKL value. But what's happening is that, say I have 10 different MATKLs in my table, and all of them enter the code within the LOOP, only some of them reach the END OF code - the rest simply stops in the AT END OF line, then skips right away to ENDLOOP, continuing with the next line of the table (debugging).

There is no header line, no TABLES statement declared... this is a STANDARD TABLE.

Anyone has any idea of what I'm missing here ?

Thanks in advance,

Avraham

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,498

Hi,

The reason for your problem is as follows.

assume your internal table (I_DATA)

values as follows.

MATKL DATE

yours values.

140 12/12/2008

140 13/12/2008

140 14/12/2008

150 12/12/2008

150 13/12/2008

160 12/12/2008

160 13/12/2008

160 14/12/2008

170 12/12/2008

170 13/12/2008

If your loop is like the below.

loop at i_data where date <= 13/12/2008.

endloop.

The loop will not go to the last entry for MATKL "140" and "160" ,

so the AT END OF MATKL will trigger for the MATKL "150" and "170".

Hope this will help you.

Regards,

Smart Varghese

Hi there,

I'm having a weird problem with the END OF option of the AT statement. I already knew that it can get tricky because it compares not only the specified field's content after AT END OF, but also all the fields to its left, in the structure.

But this still doesn't answer why am I getting very weird results here.

Basically, this is my scenario:

My internal table:

MATKL COLUMN B COLUMN C

140 xxxxxx yyyyyyy

140 cfsdd dasdsa

140 pdasd oasdas

150 dsadas dasdas

150 asdasd asdasd

.

.

170 xxxx xxxx

170 xxx xxxx

.

.

The code I'm having problems with is:

LOOP AT my_tab INTO my_struct WHERE date <= p_date.
    ....code
    ....code

    AT END OF matkl .
      CLEAR ls_t023t.
      READ TABLE gt_t023t INTO ls_t023t WITH KEY matkl = my_struct-matkl spras = sy-langu.
      ls_alv_rep-wgbez = ls_t023t-wgbez.
      APPEND ls_alv_rep TO gt_alv_rep.
      CLEAR ls_alv_rep.
    ENDAT.
  ENDLOOP.

The table is, of course, sorted by MATKL, and there are no fields left to MATKL.

For some reason, the code within the END OF is only being triggered for a small set of the MATKLs from my table. Of course, I'm taking into account the WHERE clause in the LOOP statement, but what I'm saying is that once the code enters the loop, I'd expect that each MATKL the loop processes, eventually enters the END OF code, after the last line for the given MATKL value. But what's happening is that, say I have 10 different MATKLs in my table, and all of them enter the code within the LOOP, only some of them reach the END OF code - the rest simply stops in the AT END OF line, then skips right away to ENDLOOP, continuing with the next line of the table (debugging).

There is no header line, no TABLES statement declared... this is a STANDARD TABLE.

Anyone has any idea of what I'm missing here ?

Thanks in advance,

Avraham

5 REPLIES 5
Read only

Former Member
0 Likes
1,498

sort ur internal with matkl and date.

your problem might get solved

Read only

Former Member
0 Likes
1,498

just to troubleshoot, I would remove the WHERE clause in the LOOP statement to see if that fixes your issue.

As you stated, AT statements are tricky enough, but you've introduced another layer of complexity with the WHERE clause.

Also, you may want to remove the AT END OF completely and simply check the old & new MATKL values manually - then trigger your code.

if old_matkl <> new_matkl.

endif.

Read only

Former Member
0 Likes
1,498

sort first.

Read only

venkat_o
Active Contributor
0 Likes
1,498

Hi Avraham Kahana ,

<li>SORT internal table by MATKL before you use AT END events within LOOP-ENDLOOP.

Thanks

Venkat.O

Read only

Former Member
0 Likes
1,499

Hi,

The reason for your problem is as follows.

assume your internal table (I_DATA)

values as follows.

MATKL DATE

yours values.

140 12/12/2008

140 13/12/2008

140 14/12/2008

150 12/12/2008

150 13/12/2008

160 12/12/2008

160 13/12/2008

160 14/12/2008

170 12/12/2008

170 13/12/2008

If your loop is like the below.

loop at i_data where date <= 13/12/2008.

endloop.

The loop will not go to the last entry for MATKL "140" and "160" ,

so the AT END OF MATKL will trigger for the MATKL "150" and "170".

Hope this will help you.

Regards,

Smart Varghese