關於AT NEW

1. 關於AT NEW 示例以下:app

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: 中間循環內容正常,這個以前一直沒注意~~~spa

解決辦法: 1. 賦值:wa_data = it_data.   2. LOOP AT it_data ASSIGNING <wa_data> 我的比較喜歡用,指針處理。指針

 

2. 一次處理多級別循環問題:示例以下code

  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 級別,節省循環次數. 測試了下,前後順序對彙總沒有影響。blog

起做用的值有AT NEW和AT END OF.it

相關文章
相關標籤/搜索