ABAP 动态生成内表的几种方法(Several Methods of Dynamic Internal Table Creation)
最近要写个程序,既有更新的,也有删除的,需要涉及到很多系统表,如果一个表一个表进行更新或者删除太慢了,于是就想通过创建动态内表来实现这些功能,在网上找了一些资料,经过多次尝试,终于测试成功了。网上讲述的创建动态内表的方法大致有两种。我做了一下测试,源代码附后。在这里提醒大家一点,在做动态更新程序的时候,要注意两点:
1.使用modify (p_tabname) from <dyn_wa>. 的时候,一定要确定要更新的字段是不是系统表的关键字段,如果是关键字段的话,需要先删除这条记录,delete (p_tabname) from <dyn_wa>.再进行modify,因为modify进行更新的时候如果不存在就新增,这点一定要注意。
2.如果使用UPDATE进行更新,对于非主键字段的改变还是可以的,但是对于主键字段,只能用MODIFY了。直接对SAP数据做UPDATE和MODIFY是很危险的,不到万不得已,最好不要做这些操作。如果万一出问题,会很严重的。这里就不用更新和删除程序作为测试程序了,直接做一个动态内表ALV显示的测试程序,道理和更新与删除差不多,希望对有这方面需求的朋友有所帮助!
3.最近从同事程序中发现另一种创建动态内表的简便方法,附3中为测试代码。觉得这个方法更简单些……
附1:
REPORT ZCL_TEST026.
TYPE-POOLS:abap.
PARAMETERS p_name TYPE tabname.
DATA lt_table TYPE TABLE OF dfies. "字段结构表
DATA ls_table TYPE dfies.
DATA lr_struc TYPE REF TO cl_abap_structdescr.
DATA lr_table TYPE REF TO cl_abap_tabledescr.
DATA lr_type TYPE REF TO cl_abap_typedescr.
DATA lr_data TYPE REF TO cl_abap_datadescr.
DATA lt_comp TYPE abap_component_tab.
DATA ls_comp LIKE LINE OF lt_comp.
DATA dyn_wa TYPE REF TO data.
DATA dyn_table TYPE REF TO data.
DATA l_string TYPE string.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa> TYPE ANY.
*1.获取表中的字段结构
CALL FUNCTION 'DDIF_NAMETAB_GET'
EXPORTING
tabname = p_name
TABLES
dfies_tab = lt_table
EXCEPTIONS
not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
LOOP AT lt_table INTO ls_table.
CONCATENATE ls_table-tabname '-' ls_table-fieldname INTO l_string.
ls_comp-name = ls_table-fieldname.
*读取字段类型
CALL METHOD cl_abap_datadescr=>describe_by_name
EXPORTING
p_name = l_string
RECEIVING
p_descr_ref = lr_type
EXCEPTIONS
type_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ls_comp-type ?= lr_type.
APPEND ls_comp TO lt_comp.
CLEAR ls_comp.
ENDLOOP.
*根据字段目录创建动态结构类型
CALL METHOD cl_abap_structdescr=>create
EXPORTING
p_components = lt_comp
RECEIVING
p_result = lr_struc.
*根据动态结构创建动态内表类型
CALL METHOD cl_abap_tabledescr=>create
EXPORTING
p_line_type = lr_struc
RECEIVING
p_result = lr_table.
*参照动态结构类型和动态内表类型创建内表与工作区
CREATE DATA dyn_wa TYPE HANDLE lr_struc.
CREATE DATA dyn_table TYPE HANDLE lr_table.
*指定内表与工作区到字段符号
ASSIGN dyn_wa->* TO <dyn_wa>.
ASSIGN dyn_table->* TO <dyn_table>.
*从动态表中取数到动态内表中
SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table> UP TO 100
ROWS FROM (p_name).
*显示内表中的数据
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_structure_name = p_name
TABLES
t_outtab = <dyn_table>
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
附2:
REPORT ZCL_TEST027.
TYPE-POOLS:abap.
PARAMETERS p_name TYPE tabname.
DATA: d_ref TYPE REF TO data,
lt_alv_cat TYPE TABLE OF lvc_s_fcat,
ls_alv_cat LIKE LINE OF lt_alv_cat.
DATA: lt_table LIKE TABLE OF dntab.
DATA: ls_table TYPE dntab.
FIELD-SYMBOLS : <dyn_table> TYPE table,
<dyn_wa> TYPE ANY,
<dyn_field> TYPE ANY.
*取出表结构的字段目录
CALL FUNCTION 'NAMETAB_GET'
EXPORTING
langu = sy-langu
tabname = p_name
TABLES
nametab = lt_table
EXCEPTIONS
no_texts_found = 1.
*根据取出的字段目录生成参考字段目录
LOOP AT lt_table INTO ls_table.
ls_alv_cat-fieldname = ls_table-fieldname.
ls_alv_cat-ref_table = p_name.
ls_alv_cat-ref_field = ls_table-fieldname.
APPEND ls_alv_cat TO lt_alv_cat.
CLEAR ls_alv_cat.
ENDLOOP.
*内表创建
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_alv_cat
IMPORTING
ep_table = d_ref.
*指定生成的内表到字段符号
ASSIGN d_ref->* TO <dyn_table>.
*从动态表中取数到动态内表中
SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table> UP TO 100
ROWS FROM (p_name).
*显示内表中的数据
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_structure_name = p_name
TABLES
t_outtab = <dyn_table>
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
附3:
REPORT ZCL_TEST028.
PARAMETERS p_name TYPE tabname.
DATA: dyn_table TYPE REF TO data.
DATA: dyn_wa TYPE REF TO data.
FIELD-SYMBOLS: <dyn_table> TYPE table,
<dyn_wa> TYPE ANY.
*创建动态表结构
CREATE DATA dyn_table TYPE TABLE OF (p_name).
*创建动态内表
ASSIGN dyn_table->* TO <dyn_table>.
*创建动态工作区结构
CREATE DATA dyn_wa LIKE LINE OF <dyn_table>.
*创建动态工作区
ASSIGN dyn_wa->* TO <dyn_wa>.
*从动态表中取数到动态内表中
SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table> UP TO 100
ROWS FROM (p_name).
*对取出数据进行处理
LOOP AT <dyn_table> assigning <dyn_wa>.
"内表数据处理
ENDLOOP.
*显示内表中的数据
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_structure_name = p_name
TABLES
t_outtab = <dyn_table>
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
转自:http://www.cnblogs.com/clsoho/archive/2010/07/23/1783851.html
ABAP动态内表使用的例子
*&---------------------------------------------------------------------*
*& Report ZLEIY_ABAP_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZLEIY_ABAP_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