【ABAP】关于AT NEW

1. 关于AT NEW 示例如下:

REPORT zfr_test.
DATA: begin of it_data OCCURS 0,
        id type char10,
        name type char10,
        menge type menge_d,
      end of it_data.

DATA: begin of it_data_sum OCCURS 0,
        id type char10,
        name type char10,
        menge type menge_d,
      end of it_data_sum.


it_data-id = 'A'.
it_data-name = 'A1'.
it_data-menge = 10.
append it_data.
it_data-id = 'A'.
it_data-name = 'B1'.
it_data-menge = 5.
append it_data.
it_data-id = 'B'.
it_data-name = 'A1'.
it_data-menge = 6.
append it_data.
it_data-id = 'B'.
it_data-name = 'A1'.
it_data-menge = 7.
append it_data.
it_data-id = 'C'.
it_data-name = 'A1'.
it_data-menge = 8.
append it_data.

SORT it_data By id.

LOOP AT it_data .
  AT NEW id.
    clear: it_data_sum.
  ENDAT.
  it_data_sum-menge = it_data_sum-menge + it_data-menge.
  AT END OF id.
    it_data_sum-id = it_data-id.
    it_data_sum-name = it_data-name.
    APPEND it_data_sum.
  ENDAT.
ENDLOOP.

针对以上问题,会导致it_data-name的值为*号,原因是,AT NEW id ... ENDAT和AT END OF id ... ENDAT中间的主键后字段,显示为*号。

细想下确实也不好显示值。 PS: 中间循环内容正常,这个之前一直没注意~~~

解决办法: 1. 赋值:wa_data = it_data.   2. LOOP AT it_data ASSIGNING <wa_data> 个人比较喜欢用,指针处理。

 

2. 一次处理多级别循环问题:示例如下

  REPORT zfr_test.
  DATA: BEGIN OF it_data OCCURS 0,
          id TYPE char10,
          name TYPE char10,
          menge TYPE menge_d,
        END OF it_data.
  DATA: BEGIN OF it_data_sum OCCURS 0,
          id TYPE char10,
          name TYPE char10,
          menge TYPE menge_d,
        END OF it_data_sum.
  DATA: BEGIN OF it_test_sum OCCURS 0,
          id TYPE char10,
          name TYPE char10,
          menge TYPE menge_d,
        END OF it_test_sum.
  FIELD-SYMBOLS <wa_data> LIKE LINE OF it_data.

  it_data-id = 'A'.
  it_data-name = 'A1'.
  it_data-menge = 10.
  APPEND it_data.
  it_data-id = 'A'.
  it_data-name = 'B1'.
  it_data-menge = 5.
  APPEND it_data.
  it_data-id = 'B'.
  it_data-name = 'A1'.
  it_data-menge = 6.
  APPEND it_data.
  it_data-id = 'B'.
  it_data-name = 'A1'.
  it_data-menge = 7.
  APPEND it_data.
  it_data-id = 'C'.
  it_data-name = 'A1'.
  it_data-menge = 8.
  APPEND it_data.

SORT it_data BY id name.

BREAK-POINT.
LOOP AT it_data ASSIGNING <wa_data> .
  AT NEW id.
    CLEAR: it_data_sum.
  ENDAT.
  AT NEW name.
    CLEAR it_test_sum.
  ENDAT.

  it_data_sum-menge = it_data_sum-menge + <wa_data>-menge.
  it_test_sum-menge = it_test_sum-menge + <wa_data>-menge.

  AT END OF  name.
    it_test_sum-id = <wa_data>-id.
    it_test_sum-name = <wa_data>-name.
    APPEND it_test_sum.
  ENDAT.
  AT END OF id.
    it_data_sum-id = <wa_data>-id.
    it_data_sum-name = <wa_data>-name.
    APPEND it_data_sum.
  ENDAT.
ENDLOOP.

针对一次循环处理多重汇总的问题,可以汇总到id级别 和 id+name 级别,节省循环次数. 测试了下,先后顺序对汇总没有影响。

起作用的值有AT NEW和AT END OF.

 

posted on 2015-10-07 13:34  color_story  阅读(679)  评论(0编辑  收藏  举报

导航