AT END OF statement
always triggers when there is any change of fields’ data. This statement
triggers at the last occurrence of that value. Let’s take an example. We have a
table MARD which contains material, plant & storage location. Now we want
to trigger AT END OF at the last occurrence of plant data as follows.
As we can see that the
statement triggers at the last occurrence of plant data.
REPORT zctrlbrk_at_end_of NO STANDARD PAGE HEADING.
TYPES: BEGIN OF ty_tab,
werks TYPE mard-werks,
matnr TYPE mard-matnr,
lgort TYPE mard-lgort,
END OF ty_tab.
DATA: wtab TYPE ty_tab,
itab TYPE TABLE OF ty_tab.
START-OF-SELECTION.
SELECT matnr werks lgort
UP TO 30 ROWS FROM mard
INTO CORRESPONDING FIELDS OF TABLE itab.
SORT itab BY werks.
WRITE: / 'Material
Number', 20 'Plant', 27 'Storage Location'.
ULINE.
LOOP AT itab INTO wtab.
WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort.
AT END OF werks.
WRITE: '=== At End Of plant - triggers at line ', sy-tabix.
ENDAT.
ENDLOOP.
Here we have shifted
the plant (WERKS) at field 1 in structure level. Otherwise system will consider
material & plant both as a key. Because the AT END OF will trigger if there
is any change from left most field to the particular field.
Now we want to see at
the debugging level of AT END OF statement. On second line it has triggered
because that is the last occurrence of plant 1200. SY-TABIX = 2. For the rest
of the rows system will skip the AT END OF statement.
Similarly it has
triggered at last occurrence of plant 3000. SY-TABIX = 15.
There is only one
entry for plant 7500. So it triggers at the next loop iteration. SY-TABIX = 16.
At the last of the
table record AT END OF triggers. Here the plant occurrence & table data
both are of last entry.
The output is as
follows:
TYPES: BEGIN OF ty_tab,
werks TYPE mard-werks,
matnr TYPE mard-matnr,
lgort TYPE mard-lgort,
END OF ty_tab.
DATA: wtab TYPE ty_tab,
itab TYPE TABLE OF ty_tab.
START-OF-SELECTION.
SELECT matnr werks lgort
UP TO 30 ROWS FROM mard
INTO CORRESPONDING FIELDS OF TABLE itab.
SORT itab BY werks.
WRITE: / 'Material Number', 20 'Plant', 27 'Storage Location'.
ULINE.
LOOP AT itab INTO wtab.
WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort.
AT END OF werks.
WRITE: '=== At End Of plant - triggers at line ', sy-tabix.
ENDAT.
ENDLOOP.
No comments:
Post a Comment