Control Break - AT NEW statement
AT NEW is one of a control break statements. It works
inside the LOOP – ENDLOOP. Let’s take an example. We have prepared an internal
table where we are storing 50 rows of material, plant & storage location
data. Now we run the operation of AT NEW for plant record. Whenever the system
finds a new plant with respect to previous then it will trigger the AT NEW
statement. Here the internal table must be sorted. Otherwise similar plant may
come afterwards and system will trigger AT NEW wrongly.
The program 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.
IF sy-subrc = 0.
SORT itab.
WRITE: / 'Material', 20 'Plant', 27 'Storage Location'.
ULINE.
LOOP AT itab INTO wtab.
WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort.
AT NEW werks.
WRITE: '***** AT NEW plant triggers at ', sy-tabix.
ENDAT.
ENDLOOP.
ENDIF.
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.
IF sy-subrc = 0.
SORT itab.
WRITE: / 'Material', 20 'Plant', 27 'Storage Location'.
ULINE.
LOOP AT itab INTO wtab.
WRITE: / wtab-matnr, 20 wtab-werks, 27 wtab-lgort.
AT NEW werks.
WRITE: '***** AT NEW plant triggers at ', sy-tabix.
ENDAT.
ENDLOOP.
ENDIF.
Now we shall see at debugging mode how AT NEW works. At
the first loop iteration AT NEW will definitely trigger because the plant is
read as new. Here SY-TABIX = 1.
After entering into AT NEW, we can see the other fields
which are right side of the mentioned field contain ***. Only the mentioned field
contains the proper data.
After that whenever system finds a new plant data it will
trigger AT NEW similarly. Here at SY-TABIX = 3.
Similarly at SY-TABIX = 16 it will be triggered.
In this way AT NEW is triggered inside the loop. The
output is as follows.