ABAP-如何读取内表的字段名称

*&---------------------------------------------------------------------*
*& 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
 
 
 
 
 
http://www.itpub.net/thread-1039193-1-1.html
 
rockwl2001:获得内表中的字段名,然后取VKORG、VTWEG、SPART三个字段的值,再做些操作,所有过程都是动态的。
 
 
 
 
 
*&---------------------------------------------------------------------*
*&      Form  sd_itab_check_vkorg
*&---------------------------------------------------------------------*
*       根据销售范围的权限删去内表中没有权限的值
*----------------------------------------------------------------------*
*      -->I_ITAB     text
*----------------------------------------------------------------------*
FORM sd_itab_check_vkorg CHANGING i_itab TYPE INDEX TABLE.
  DATA: l_tabcount TYPE i,
        l_tabledescr_ref TYPE REF TO cl_abap_tabledescr,
        l_descr_ref      TYPE REF TO cl_abap_structdescr,
  l_indexof_vkorg TYPE i,
  l_indexof_vtweg TYPE i,
  l_indexof_spart TYPE i.
  FIELD-SYMBOLS: <fs_wa> TYPE ANY,
                 <fs_comp_wa> TYPE abap_compdescr,
                 <fs_vkorg> TYPE ANY,
                 <fs_vtweg> TYPE ANY,
                 <fs_spart> TYPE ANY.

  DESCRIBE TABLE i_itab LINES l_tabcount.
  IF l_tabcount <= 0.
    EXIT.
  ENDIF.
  l_tabledescr_ref ?= cl_abap_typedescr=>describe_by_data( i_itab ).
  l_descr_ref ?= l_tabledescr_ref->get_table_line_type( ).
  LOOP AT l_descr_ref->components ASSIGNING <fs_comp_wa>.
    IF <fs_comp_wa>-name = 'VKORG'.
      l_indexof_vkorg = sy-tabix.
    ELSEIF <fs_comp_wa>-name = 'VTWEG'.
      l_indexof_vtweg = sy-tabix.
    ELSEIF <fs_comp_wa>-name = 'SPART'.
      l_indexof_spart = sy-tabix.
    ENDIF.
    IF l_indexof_vkorg IS NOT INITIAL AND l_indexof_vtweg IS NOT INITIAL AND
       l_indexof_spart IS NOT INITIAL.
      EXIT.
    ENDIF.
  ENDLOOP.
  IF l_indexof_vkorg IS INITIAL OR l_indexof_vtweg IS INITIAL OR
     l_indexof_spart IS INITIAL.
    RAISE EXCEPTION TYPE cx_sy_table_attributes.
  ENDIF.
  LOOP AT i_itab ASSIGNING <fs_wa>.
    ASSIGN COMPONENT l_indexof_vkorg OF STRUCTURE <fs_wa> TO <fs_vkorg>.
    ASSIGN COMPONENT l_indexof_vtweg OF STRUCTURE <fs_wa> TO <fs_vtweg>.
    ASSIGN COMPONENT l_indexof_spart OF STRUCTURE <fs_wa> TO <fs_spart>.
    AUTHORITY-CHECK OBJECT 'V_VBAK_VKO'
             ID 'VKORG' FIELD <fs_vkorg>
             ID 'VTWEG' FIELD <fs_vtweg>
             ID 'SPART' FIELD <fs_spart>
             ID 'ACTVT' FIELD '03'.
    IF sy-subrc NE 0.
      DELETE i_itab INDEX sy-tabix.
    ENDIF.
  ENDLOOP.
ENDFORM.                    "sd_itab_check_vkorg
 
 
 
dreamgift:在做ALV中有个  REUSE_ALV_FIELDCATALOG_MERGE    可以得到内表中的项目名.
 
xiner418:简单的说就是在传进来的参数外面加括号,就可以了

 

转载于:https://www.cnblogs.com/Nirvanacafe/p/4449649.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值