SAP动态生成内表结构及其内容
REPORT zdyn_test.
FIELD-SYMBOLS:
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. \
*&---------------------------------------------------------------------* *& 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
*&---------------------------------------------------------------------* *& 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
ASSIGN wa_new_line->* TO
\用
LOOP AT it_structure INTO wa_structure. j = j + 1.
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE
APPEND
ENDFORM. \
*&---------------------------------------------------------------------* *& Form output_dyntable_data
*&---------------------------------------------------------------------* FORM output_dyntable_data .
LOOP AT it_structure INTO wa_structure. WRITE: wa_structure-fieldname(5). ENDLOOP.
LOOP AT
LOOP AT it_structure INTO wa_structure.
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE