最近要写个程序,既有更新的,也有删除的,需要涉及到很多系统表,如果一个表一个表进行更新或者删除太慢了,于是就想通过创建动态内表来实现这些功能,在网上找了一些资料,经过多次尝试,终于测试成功了。网上讲述的创建动态内表的方法大致有两种。我做了一下测试,源代码附后。在这里提醒大家一点,在做动态更新程序的时候,要注意两点:
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.