乐趣区

关于abap:ABAP-动态内表实现方式-灵活运用

FORM frm_create_compose_dymic_tab  USING ev_error ev_message TYPE bapi_msg.
  DATA l_line TYPE char10.
  DATA:
    lo_elemt TYPE REF TO cl_abap_elemdescr,
    lt_comp  TYPE cl_abap_structdescr=>component_table,
    ls_comp  LIKE LINE OF lt_comp.

  DATA:lr_dref  TYPE REF TO data,
       lo_descr TYPE REF TO cl_abap_structdescr,
       l_string TYPE string.

  DATA: lo_struc TYPE REF TO cl_abap_structdescr,
        lo_table TYPE REF TO cl_abap_tabledescr,
        lo_type  TYPE REF TO cl_abap_typedescr,
        lo_line  TYPE REF TO data,
        lo_itab  TYPE REF TO data.

  DATA(lv_tabname) = '/BIC/AZO_BCD1100'.
  CREATE DATA lr_dref TYPE (lv_tabname).
  ASSIGN lr_dref->* TO FIELD-SYMBOL(<fs_data>).

  lo_descr ?= cl_abap_typedescr=>describe_by_data(<fs_data>).
  DATA(lt_fields) = lo_descr->get_ddic_field_list( p_langu = sy-langu
                                             p_including_substructres = abap_true ).

  LOOP AT lt_fields ASSIGNING FIELD-SYMBOL(<fs_fields>).

    IF <fs_fields>-fieldname = 'FISCYEAR'        OR
       <fs_fields>-fieldname = 'FISCPER3'         OR
       <fs_fields>-fieldname = 'FISCVARNT'        OR
       <fs_fields>-fieldname = '/BIC/ZBPC_GUID'   OR
       <fs_fields>-fieldname = 'RECORDMODE'       OR
       <fs_fields>-fieldname = '/BIC/ZBPC_NO'     OR
       <fs_fields>-fieldname = 'CURRENCY'.
      CONTINUE.
    ELSEIF <fs_fields>-fieldname = '/BIC/ZBPC_TK32'.
      CONCATENATE <fs_fields>-tabname '-' <fs_fields>-fieldname INTO l_string.
      ls_comp-name = <fs_fields>-fieldname.
      CALL METHOD cl_abap_datadescr=>describe_by_name
        EXPORTING
          p_name         = l_string
        RECEIVING
          p_descr_ref    = lo_type
        EXCEPTIONS
          type_not_found = 1
          OTHERS         = 2.
      ls_comp-type ?= lo_type.
      APPEND ls_comp TO lt_comp.
      CLEAR ls_comp.

*      ls_comp-name = <fs_fields>-fieldname.
      CONCATENATE <fs_fields>-fieldname '1' INTO ls_comp-name.
      ls_comp-type ?= cl_abap_elemdescr=>get_c(p_length = 20).
      APPEND ls_comp TO lt_comp.
      CLEAR ls_comp.
      CONTINUE.
    ENDIF.

    CONCATENATE <fs_fields>-tabname '-' <fs_fields>-fieldname INTO l_string.
    ls_comp-name = <fs_fields>-fieldname.
    CALL METHOD cl_abap_datadescr=>describe_by_name
      EXPORTING
        p_name         = l_string
      RECEIVING
        p_descr_ref    = lo_type
      EXCEPTIONS
        type_not_found = 1
        OTHERS         = 2.
    ls_comp-type ?= lo_type.
    APPEND ls_comp TO lt_comp.
    CLEAR ls_comp.
  ENDLOOP.


  IF lt_comp[] IS INITIAL.
    ev_error = 'E'.
    ev_message = '没有保护请查看输出表头'.
  ELSE.
    TRY.
        lo_struc = cl_abap_structdescr=>create(lt_comp).
      CATCH cx_sy_struct_attributes .
    ENDTRY.
****  end
*Generate table type dynamically
    TRY.
        CALL METHOD cl_abap_tabledescr=>create
          EXPORTING
            p_line_type  = lo_struc
            p_table_kind = 'S'
            p_key_kind   = 'D'
          RECEIVING
            p_result     = lo_table.
      CATCH cx_sy_table_creation .
    ENDTRY.
*Generate internal table via the dynamic table type
    CREATE DATA lo_itab TYPE HANDLE lo_table.
    ASSIGN lo_itab->* TO <fs_itab>.
*Generate work area
    CREATE DATA lo_line TYPE HANDLE lo_struc.
    ASSIGN lo_line->* TO <fs_wa>.
  ENDIF.
ENDFORM.
退出移动版