【转载】ABAP-如何读取内表的字段名称

时间:2023-03-08 16:55:30
*&---------------------------------------------------------------------*
*& Report  ZTRAINING29 如何得到内表的字段名称与字段类型                *
*& T-code                                                              *
*&---------------------------------------------------------------------*
*& Created by Xavery hsueh(薛现军) on 2011-06-08                       *
*& Last edited date:                                                   *
*&---------------------------------------------------------------------*
REPORT  ztraining29 NO STANDARD PAGE HEADING .

************************************************************************
**  声明数据库表 Declaration of database                              **
************************************************************************
TABLES:mara,
       makt.    "
************************************************************************
**  定义结构类型 Define the structure's type                          **
************************************************************************
*     物料编号的内表
TYPES:BEGIN OF typ_mara,
        matnr TYPE matnr,
        meins TYPE meins,
        maktx TYPE maktx,
      END OF typ_mara.
*     保存内表的字段名称
TYPES:BEGIN OF typ_field,
        fieldnm TYPE txt30,
      END OF typ_field.
************************************************************************
**  定义变量与内表 Define the variants and Internal tables            **
************************************************************************
DATA:gt_mara TYPE TABLE OF typ_mara WITH HEADER LINE.
DATA:gt_field TYPE TABLE OF typ_field WITH HEADER LINE.

DATA:cl_descr TYPE REF TO cl_abap_structdescr.

FIELD-SYMBOLS:<fs_comp> TYPE abap_compdescr.
FIELD-SYMBOLS <fs_name> TYPE ANY.
************************************************************************
**  宏定义 Define the macro                                           **
************************************************************************
DEFINE mcr_range.
  clear &1.
  &1-sign = 'I'.
  &1-option = &2.
  &1-low = &3.
  &1-high = &4.
  append &1.
END-OF-DEFINITION.
************************************************************************
**  选择屏幕 Customize the selection-screen                           **
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK xavery WITH FRAME TITLE text_001.
PARAMETERS: p_erdat TYPE erdat DEFAULT sy-datum.      "统计日期
SELECT-OPTIONS s_matnr FOR mara-matnr.                "物料编号
SELECTION-SCREEN END OF BLOCK xavery.
************************************************************************
**  执行程序事件 Executing the program's events                       **
************************************************************************
INITIALIZATION.
  PERFORM sub_init_cond.

START-OF-SELECTION.
  PERFORM sub_process_fieldname.

END-OF-SELECTION.
*@---------------------------------------------------------------------*
*@      Form  SUB_INIT_COND
*@---------------------------------------------------------------------*
*       初始化选择条件
*----------------------------------------------------------------------*
FORM sub_init_cond .
  text_001 = '选择屏幕'.
ENDFORM.                    " SUB_INIT_COND
*&---------------------------------------------------------------------*
*&      Form  sub_process_fieldname
*&---------------------------------------------------------------------*
*       取得内表字段名称与类型
*----------------------------------------------------------------------*
FORM sub_process_fieldname .
  DATA:g_fieldnm TYPE txt30.
  cl_descr ?= cl_abap_typedescr=>describe_by_data( gt_mara ).
  LOOP AT cl_descr->components ASSIGNING <fs_comp>.
    WRITE: / <fs_comp>-name,             "字段名称
             <fs_comp>-type_kind,        "字段类型
             <fs_comp>-length,           "字段长度
             <fs_comp>-decimals.         "字段小数位
    APPEND <fs_comp>-name TO gt_field.
  ENDLOOP.

LOOP AT gt_field.
    CONCATENATE 'GT_MARA-' gt_field-fieldnm INTO gt_field.
    MODIFY gt_field.
  ENDLOOP.

SELECT * FROM mara
      INTO CORRESPONDING FIELDS OF TABLE gt_mara
      WHERE matnr IN s_matnr.
* 使用得到的字段名称,输出字段结果
  LOOP AT gt_mara.
    WRITE /.
    LOOP AT gt_field.
      ASSIGN (gt_field-fieldnm) TO <fs_name>.
      WRITE: <fs_name>.
    ENDLOOP.
  ENDLOOP.
ENDFORM.                    " sub_process_fieldname