欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SAP 动态内表demo----自用

程序员文章站 2022-05-18 08:03:52
...
*&---------------------------------------------------------------------*
*& Report Z_TEST
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z_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

 

相关标签: SAP