Posted: Wed Oct 24, 2007 7:08 pm Post subject: Как прочитать структуру внутренней таблицы?
Есть код
Code:
DATA: BEGIN OF wa_sum,
BUKRS TYPE BSEG-BUKRS,
BELNR TYPE BSEG-BELNR,
GJAHR TYPE BSEG-GJAHR,
BUZEI TYPE BSEG-BUZEI,
DMBTR TYPE BSEG-DMBTR,
WRBTR TYPE BSEG-WRBTR,
SHKZG TYPE BSEG-SHKZG,
END OF wa_sum.
DATA: BEGIN OF it_table OCCURS 0.
INCLUDE STRUCTURE BKPF.
DATA:
DMBTR TYPE BSEG-DMBTR,
WRBTR TYPE BSEG-WRBTR,
f_del TYPE C.
DATA: END OF it_table.
SELECT * FROM BKPF INTO TABLE it_table.
IF sy-subrc IS INITIAL.
SORT it_table BY bukrs belnr gjahr.
SELECT bukrs belnr gjahr buzei dmbtr wrbtrs shkzg FROM bseg
INTO wa_sum
FOR ALL ENTRIES IN it_table
WHERE bukrs = it_table-bukrs
AND belnr = it_table-belnr
AND gjahr = it_table-gjahr.
IF wa_sum-shkzg = 'S'.
...
ENDIF.
ENDSELECT.
ENDIF.
Как внутри программы узнать названия полей внутренней таблицы it_table?
1) Использование Функционального модуля
GET_COMPONENT_LIST
Code:
REPORT zprgm.
(...)
* Internal table
DATA : begin of itab occurs 0,
f1(5) type c,
f2(15) type c,
f3 type matnr,
end of itab.
* Buffer
DATA : work_area LIKE LINE OF itab.
(...)
Для получения имен полей структуры work_area используйте:
Code:
CALL FUNCTION 'GET_COMPONENT_LIST'
EXPORTING
program = 'ZPRGM'
fieldname = 'WORK_AREA'
TABLES
components = it_components.
В it_components будет содержаться много информации о полях структуры.
Обратите внимание: WORK_AREA должна быть определена как global в программе ZPRGM.
Example 2
Code:
REPORT Z.
DATA : BEGIN OF GT_TEST OCCURS 0,
BANFN LIKE EBAN-BANFN,
BNFPO LIKE EBAN-BNFPO,
SUM TYPE P DECIMALS 2,
LONG_FIELD_NAME(10),
END OF GT_TEST.
DATA : GT_INFO LIKE RSTRUCINFO OCCURS 0 WITH HEADER LINE,
G_REPID LIKE SY-REPID.
G_REPID = SY-REPID.
CALL FUNCTION 'GET_COMPONENT_LIST'
EXPORTING
PROGRAM = G_REPID
FIELDNAME = 'GT_TEST'
TABLES
COMPONENTS = GT_INFO
EXCEPTIONS
OTHERS = 1.
LOOP AT GT_INFO.
WRITE : / GT_INFO-COMPNAME.
ENDLOOP.
Обратите внимание на необходимость использования G_REPID вместо прямого указания SY-REPID, потому что внутри функции SY-REPID изменит значение на название пула модулей.
2) В системе 4.6 и выше существуют глобальные классы
cl_abap_tabledescr и cl_abap_structdescr
Code:
METHOD get_output_components .
DATA:
lcl_table_descr TYPE REF TO cl_abap_tabledescr,
lcl_struc_descr TYPE REF TO cl_abap_structdescr
.
FIELD-SYMBOLS:
<lt_output> TYPE STANDARD TABLE
.
ASSIGN me->output_tab->* TO <lt_output>.
lcl_table_descr ?= cl_abap_typedescr=>describe_by_data( <lt_output> ).
lcl_struc_descr ?= lcl_table_descr->get_table_line_type( ).
it_components = lcl_struc_descr->components.
ENDMETHOD.
3) Использование оператора
DESCRIBE FIELD field INTO field_descr.
Code:
TYPE-POOLS: SYDES.
DATA TD TYPE SYDES_DESC.
DESCRIBE FIELD GT_TEST INTO TD.
в таблице TD-TYPES будет характеристики полей
в таблице TD-NAMES названия полей
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.