動態內表

來源:互聯網
上載者:User

關鍵技巧:

1, 建立動態內表:

a, 動態內表的結構的定義.

動態內表表結構的定義必須使用表結構與 table type: lvc_t_fcat一樣的內表.

一般情況下,我們都內表的所有列定義成字元型.

b, 根據表結構產生內表.
系統提供了一個標準的method來產生動態表,使用方法如下:

2, 動態內表的賦值:

a, 擷取指定的欄位

b, 給指定的欄位賦值

3, 讀取動態內表的值:

a, 擷取指定的欄位

b, 讀取指定的欄位值

REPORT  zdyn_test.

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
               <dyn_wa>,
               <dyn_field>.

DATA: dy_table TYPE REF TO data,
      dy_line  TYPE REF TO data,
      it_structure TYPE lvc_t_fcat,
      wa_structure TYPE lvc_s_fcat.

START-OF-SELECTION.
  PERFORM create_structure.  " 定義內表的結構

  PERFORM create_dynamic_table.  " 按照定義的內表結構,產生一個內表

  PERFORM write_data_to_dyntable.  " 向動態內表中寫數

  PERFORM output_dyntable_data.   " 從動態內表中取數,並寫到螢幕

*&---------------------------------------------------------------------*
*&      Form  create_structure
*&---------------------------------------------------------------------*

FORM create_structure .
  wa_structure-fieldname = 'COL1'.  " 第一列列名
  wa_structure-col_pos   = 1.       " 表示第一列 --- 可心省略,預設情況下,第一行對應到生產內表的第一列,如果指定,則按指定的列順序產生內表
  wa_structure-inttype = 'C'.       " 資料類型
  wa_structure-intlen = 6.          " 長度
  APPEND wa_structure TO it_structure.

  wa_structure-fieldname = 'COL2'.  " 第二列列名
  wa_structure-col_pos   = 2.       " 表示第二列--- 可心省略,預設情況下,第一行對應到生產內表的第一列,如果指定,則按指定的列順序產生內表
  wa_structure-inttype = 'C'.       " 資料類型
  wa_structure-intlen = 6.          " 長度
  APPEND wa_structure TO it_structure.

  wa_structure-fieldname = 'COL3'.  " 第三列名
  wa_structure-col_pos   = 3.       " 表示第三列 --- 可心省略,預設情況下,第一行對應到生產內表的第一列,如果指定,則按指定的列順序產生內表
  wa_structure-inttype = 'C'.       " 資料類型
  wa_structure-intlen = 6.          " 長度
  APPEND wa_structure TO it_structure.

ENDFORM.                    " create_structure
*&---------------------------------------------------------------------*
*&      Form  create_dynamic_table
*&---------------------------------------------------------------------*

FORM create_dynamic_table .

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = it_structure
    IMPORTING
      ep_table        = dy_table.

  ASSIGN dy_table->* TO <dyn_table>.    " 用表類型指標 <dyn_table> 指向 資料對象的內容.
ENDFORM.                    " create_dynamic_table

*&---------------------------------------------------------------------*
*&      Form  write_data_to_dyntable
*&---------------------------------------------------------------------*

FORM write_data_to_dyntable .
  DATA:wa_new_line TYPE REF TO data.
  DATA:i TYPE n.
  DATA:j TYPE n.
  CREATE DATA wa_new_line LIKE LINE OF <dyn_table>.  " 建立一個與動態內表結構相同的資料對象,且資料對象為是一個結構
  ASSIGN wa_new_line->* TO <dyn_wa>.

  " 用<dyn_wa>指標指向該結構
  DO 3 TIMES.
    i = i + 1.
    CLEAR j.
    LOOP AT it_structure INTO wa_structure.
      j = j + 1.
      ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>.  " 用指標 <dyn_field>指向工作區<dyn_wa>中的一個欄位,欄位名為wa_structure-fieldname.
      CONCATENATE i j INTO <dyn_field>.                                              " 給指標指向的欄位賦值
    ENDLOOP.
    APPEND <dyn_wa> TO <dyn_table>.
  ENDDO.
ENDFORM.                    " write_data_to_dyntable

*&---------------------------------------------------------------------*
*&      Form  output_dyntable_data
*&---------------------------------------------------------------------*

FORM output_dyntable_data .
  LOOP AT it_structure INTO wa_structure.
    WRITE: wa_structure-fieldname(5).
  ENDLOOP.
  LOOP AT <dyn_table> INTO <dyn_wa>.
    WRITE: / .
    LOOP AT it_structure INTO wa_structure.
      ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>.  " 用指標 <dyn_field>指向工作區<dyn_wa>中的一個欄位,欄位名為wa_structure-fieldname.
      WRITE: <dyn_field>.
    ENDLOOP.
  ENDLOOP.
ENDFORM.                    " output_dyntable_data

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.