ABAP动态⽣成内表的三种⽅法原⽂链接:blog.csdn/zhongguomao/article/details/77474375
动态内表的创建和使⽤主要有以下⼏点:
1.动态内表的创建,⾸先要定义动态结构,然后再根据定义的动态结构利⽤系统⼀个标准的method:”cl_ALV_table_create=>create_dynamic_table“⽣成动态内表。
2.动态内表的赋值,遍历动态结构,获取指定的字段,然后给指定的字段赋值。
3.动态内表的读取显⽰,与赋值相似,遍历动态结构,获取指定的字段,然后读取对应字段的值。
⽅法1:
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.tabletable
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 TO100 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:
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 TO100 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:
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 TO100 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.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论