ABAP-如何讀取內表的字段名稱
轉自http://blog.sina.com.cn/s/blog_4d1570de0100txvv.html
*&---------------------------------------------------------------------*
*& 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:簡單的說就是在傳進來的參數外面加括號,就可以了