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


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

     1 *&---------------------------------------------------------------------*
     2 *& Report  YDEMO_RICK
     3 *&
     4 *&---------------------------------------------------------------------*
     5 *&
     6 *&
     7 *&---------------------------------------------------------------------*
     8 REPORT  ydemo_rick.
     9 
    10 TYPE-POOLS:abap.
    11 PARAMETERS p_name TYPE tabname.
    12 DATA lt_table TYPE TABLE OF dfies. "字段结构表
    13 DATA ls_table TYPE dfies.
    14 DATA lr_struc TYPE REF TO cl_abap_structdescr.
    15 DATA lr_table TYPE REF TO cl_abap_tabledescr.
    16 DATA lr_type TYPE REF TO cl_abap_typedescr.
    17 DATA lr_data TYPE REF TO cl_abap_datadescr.
    18 DATA lt_comp TYPE abap_component_tab.
    19 DATA ls_comp LIKE LINE OF lt_comp.
    20 DATA dyn_wa TYPE REF TO data.
    21 DATA dyn_table TYPE REF TO data.
    22 DATA l_string TYPE string.
    23 FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
    24                <dyn_wa> TYPE any.
    25 
    26 *1.获取表中的字段结构
    27 CALL FUNCTION 'DDIF_NAMETAB_GET'
    28   EXPORTING
    29     tabname   = p_name
    30   TABLES
    31     dfies_tab = lt_table
    32   EXCEPTIONS
    33     not_found = 1
    34     OTHERS    = 2.
    35 IF sy-subrc <> 0.
    36   MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    37   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 .
    38 ENDIF.
    39 
    40 LOOP AT lt_table INTO ls_table.
    41   CONCATENATE ls_table-tabname '-' ls_table-fieldname INTO l_string.
    42   ls_comp-name = ls_table-fieldname.
    43 *读取字段类型
    44   CALL METHOD cl_abap_datadescr=>describe_by_name
    45     EXPORTING
    46       p_name         = l_string
    47     RECEIVING
    48       p_descr_ref    = lr_type
    49     EXCEPTIONS
    50       type_not_found = 1
    51       OTHERS         = 2.
    52   IF sy-subrc <> 0.
    53     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    54     WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    55   ENDIF.
    56   ls_comp-type ?= lr_type.
    57   APPEND ls_comp TO lt_comp.
    58   CLEAR ls_comp.
    59 ENDLOOP.
    60 *根据字段目录创建动态结构类型
    61 CALL METHOD cl_abap_structdescr=>create
    62   EXPORTING
    63     p_components = lt_comp
    64   RECEIVING
    65     p_result     = lr_struc.
    66 *根据动态结构创建动态内表类型
    67 CALL METHOD cl_abap_tabledescr=>create
    68   EXPORTING
    69     p_line_type = lr_struc
    70   RECEIVING
    71     p_result    = lr_table.
    72 *参照动态结构类型和动态内表类型创建内表与工作区
    73 CREATE DATA dyn_wa TYPE HANDLE lr_struc.
    74 CREATE DATA dyn_table TYPE HANDLE lr_table.
    75 *指定内表与工作区到字段符号
    76 ASSIGN dyn_wa->* TO <dyn_wa>.
    77 ASSIGN dyn_table->* TO <dyn_table>.
    78 
    79 *从动态表中取数到动态内表中
    80 SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table> UP TO 100
    81 ROWS FROM (p_name).
    82 *显示内表中的数据
    83 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    84   EXPORTING
    85     i_structure_name = p_name
    86   TABLES
    87     t_outtab         = <dyn_table>
    88   EXCEPTIONS
    89     program_error    = 1
    90     OTHERS           = 2.
    91 IF sy-subrc <> 0.
    92   MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    93   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    94 ENDIF.
    View Code

    附2:

     1 *&---------------------------------------------------------------------*
     2 *& Report  YDEMO_RICK
     3 *&
     4 *&---------------------------------------------------------------------*
     5 *&
     6 *&
     7 *&---------------------------------------------------------------------*
     8 REPORT  ydemo_rick.
     9 
    10 TYPE-POOLS:abap.
    11 PARAMETERS p_name TYPE tabname.
    12 DATA: d_ref TYPE REF TO data,
    13 lt_alv_cat TYPE TABLE OF lvc_s_fcat,
    14 ls_alv_cat LIKE LINE OF lt_alv_cat.
    15 
    16 DATA: lt_table LIKE TABLE OF dntab.
    17 DATA: ls_table TYPE dntab.
    18 
    19 FIELD-SYMBOLS : <dyn_table> TYPE table,
    20                 <dyn_wa> TYPE any,
    21                 <dyn_field> TYPE any.
    22 *取出表结构的字段目录
    23 CALL FUNCTION 'NAMETAB_GET'
    24   EXPORTING
    25     langu          = sy-langu
    26     tabname        = p_name
    27   TABLES
    28     nametab        = lt_table
    29   EXCEPTIONS
    30     no_texts_found = 1.
    31 *根据取出的字段目录生成参考字段目录
    32 LOOP AT lt_table INTO ls_table.
    33   ls_alv_cat-fieldname = ls_table-fieldname.
    34   ls_alv_cat-ref_table = p_name.
    35   ls_alv_cat-ref_field = ls_table-fieldname.
    36   APPEND ls_alv_cat TO lt_alv_cat.
    37   CLEAR ls_alv_cat.
    38 ENDLOOP.
    39 *内表创建
    40 CALL METHOD cl_alv_table_create=>create_dynamic_table
    41   EXPORTING
    42     it_fieldcatalog = lt_alv_cat
    43   IMPORTING
    44     ep_table        = d_ref.
    45 *指定生成的内表到字段符号
    46 ASSIGN d_ref->* TO <dyn_table>.
    47 *从动态表中取数到动态内表中
    48 SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table> UP TO 100
    49 ROWS FROM (p_name).
    50 *显示内表中的数据
    51 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    52   EXPORTING
    53     i_structure_name = p_name
    54   TABLES
    55     t_outtab         = <dyn_table>
    56   EXCEPTIONS
    57     program_error    = 1
    58     OTHERS           = 2.
    59 IF sy-subrc <> 0.
    60   MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    61   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    62 ENDIF.
    View Code

    程序显示截图如上所示:

    附3:

     1 *&---------------------------------------------------------------------*
     2 *& Report  YDEMO_RICK
     3 *&
     4 *&---------------------------------------------------------------------*
     5 *&
     6 *&
     7 *&---------------------------------------------------------------------*
     8 REPORT  ydemo_rick.
     9 
    10 PARAMETERS p_name TYPE tabname.
    11 
    12 DATA: dyn_table TYPE REF TO data.
    13 DATA: dyn_wa TYPE REF TO data.
    14 
    15 FIELD-SYMBOLS: <dyn_table> TYPE table,
    16 <dyn_wa> TYPE any.
    17 
    18 *创建动态表结构
    19 CREATE DATA dyn_table TYPE TABLE OF (p_name).
    20 *创建动态内表
    21 ASSIGN dyn_table->* TO <dyn_table>.
    22 *创建动态工作区结构
    23 CREATE DATA dyn_wa LIKE LINE OF <dyn_table>.
    24 *创建动态工作区
    25 ASSIGN dyn_wa->* TO <dyn_wa>.
    26 
    27 *从动态表中取数到动态内表中
    28 SELECT * INTO CORRESPONDING FIELDS OF TABLE <dyn_table> UP TO 100
    29 ROWS FROM (p_name).
    30 *对取出数据进行处理
    31 LOOP AT <dyn_table> ASSIGNING <dyn_wa>.
    32   "内表数据处理
    33 ENDLOOP.
    34 *显示内表中的数据
    35 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    36   EXPORTING
    37     i_structure_name = p_name
    38   TABLES
    39     t_outtab         = <dyn_table>
    40   EXCEPTIONS
    41     program_error    = 1
    42     OTHERS           = 2.
    43 IF sy-subrc <> 0.
    44   MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    45   WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    46 ENDIF.
    View Code

    程序显示截图如上所示:

  • 相关阅读:
    安装和强行卸载fuse
    Elasticsearch snapshot 备份的使用方法 【备忘】
    MYSQL数据仓库infobright【备忘】
    Tomcat8 启动慢 Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [53,161] milliseconds
    python爬虫实例
    JDK1.8 JVM参数配置
    QQ登录用到的URL
    CAS5.3.X 配置备忘
    Nexus3忘记admin密码时的解决办法
    CentOS7利用systemctl添加自定义系统服务
  • 原文地址:https://www.cnblogs.com/ruingy/p/3921690.html
Copyright © 2020-2023  润新知