• ABAP动态生成内表的三种方法


    原文链接:https://blog.csdn.net/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.
    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:

    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:

    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.
  • 相关阅读:
    SWT界面刷新
    如何查看SWT源代码和帮助文档
    五分钟带你搞懂子网掩码
    python操作excel实用脚本
    Maven多镜像配置
    FrameWork数据权限浅析4之基于多维度配置表实现行级数据安全
    FrameWork数据权限浅析3之基于角色的配置表实现行级数据安全
    FrameWork数据权限浅析2之基于用户的配置表实现行级数据安全
    FrameWork数据权限浅析1之基于手工修改模型实现行级数据安全
    Framework元数据向导错误之BMT-MD-6001与BMT-IMP-0002
  • 原文地址:https://www.cnblogs.com/seven1314pp/p/15093614.html
Copyright © 2020-2023  润新知