ABAP内表循环事件块
AT FIRST……ENDAT
AT LAST……ENDAT
AT NEW……ENDAT
AT END OF……ENDAT
1、AT FIRST :该事件只在LOOP第一次循环时执行触发,只执行一次。
2、AT LAST : 该事件只在LOOP最后一次循环时执行触发,只执行一次。
3、AT NEW <name> : 包含字段name左侧的全部字段数据,与上一行数据不同,执行 AT NEW.
4、AT END OF <name> : 包含字段name左侧的全部字段数据,与下一行数据不同,执行 AT END OF.
注意: AT NEW 和 AT END OF 触发后,name右边的字符全部会被*代替,如果需要保持值不变,需要使用指针FIELD-SYMBOL代替表结构。
测试代码如下:
DATA : BEGIN OF gs_itab, s_id TYPE i, sname(10) TYPE c, address(20) TYPE c, english TYPE i, END OF gs_itab. DATA gt_itab LIKE TABLE OF gs_itab. DATA : BEGIN OF gs_data, s_id TYPE i, sname(10) TYPE c, s_ok TYPE i, s_no TYPE i, END OF gs_data. DATA gt_data LIKE TABLE OF gs_data. gs_itab-s_id = 1 . gs_itab-sname = '张三'. gs_itab-english = 61. APPEND gs_itab TO gt_itab. gs_itab-s_id = 2 . gs_itab-sname = '李四'. gs_itab-english = 81. APPEND gs_itab TO gt_itab. gs_itab-s_id = 2 . gs_itab-sname = '李四'. gs_itab-english = 85. APPEND gs_itab TO gt_itab. gs_itab-s_id = 1 . gs_itab-sname = '张三'. gs_itab-english = 55. APPEND gs_itab TO gt_itab. gs_itab-s_id = 1 . gs_itab-sname = '张三'. gs_itab-english = 59. APPEND gs_itab TO gt_itab. gs_itab-s_id = 4 . gs_itab-sname = '王五'. gs_itab-english = 59. APPEND gs_itab TO gt_itab. SORT gt_itab BY s_id. DATA count TYPE i. DATA count2 TYPE i. loop at gt_itab into gs_itab. AT FIRST. WRITE :'我AT FIRST 只会在LOOP AT 第一次循环的时候运行'. WRITE:/. ENDAT. write : '学号为',gs_itab-s_id,'名字为:',gs_itab-sname,'英语成绩为:' ,gs_itab-english. write:/. AT LAST. WRITE :'我AT LAST 只会在LOOP AT 最后一次循环的时候运行'. WRITE:/. ENDAT. endloop. LOOP AT gt_itab ASSIGNING FIELD-SYMBOL(<fs_itab>). AT NEW s_id. gs_data-s_id = <fs_itab>-s_id. gs_data-sname = <fs_itab>-sname. ENDAT. IF <fs_itab>-english >= 60 . count = count + 1. ELSE. count2 = count2 + 1. ENDIF. AT END OF s_id. gs_data-s_ok = count. gs_data-s_no = count2. CLEAR :count,count2. APPEND gs_data TO gt_data. CLEAR gs_data. ENDAT. ENDLOOP. LOOP AT gt_data INTO gs_data. AT FIRST. WRitE: '使用AT NEW 和AT END OF后 对数据处理的结果如下 '. WRITE:/. ENDAT. WRITE : '学号为',gs_data-s_id,'名字为:',gs_data-sname,'及格次数为:' ,gs_data-s_ok,'不及格次数为',gs_data-s_no. WRITE:/. AT LAST. WRITE :'最后祝各位武运昌隆'. ENDAT. ENDLOOP.
显示结果如图: