我们可以使用类 cl_abap_structdescr ,来实现动态获得任意内表中字段的名称和值。
示例程序如下:
REPORT ztest04.
"(1)准备工作:创建一个内表,并为之填充数据
data: lt_flight
type standard table
of sflight,
ls_flight like line
of lt_flight.
select * from sflight
into CORRESPONDING FIELDS OFTABLE
lt_flight where connid =
'0017' .
"(2)获得内表中的字段名称和值
data: descr_def
type ref to cl_abap_structdescr,
wa_comp type abap_compdescr.
field-symbols: <fs_in_tab>
type table,
<fs_wa> type any,
<fs_sym> type any,
<l_fld> type any.
assign lt_flight to <fs_in_tab>.
loop at <fs_in_tab>
assigning <fs_sym>.
write : /
'内表 第' , sy-tabix
,'行'.
descr_def ?= cl_abap_structdescr=>describe_by_data(<fs_sym>
).
loop at descr_def->componentsinto
wa_comp.
assign component wa_comp-nameof
structure <fs_sym> to
<l_fld>.
write : / wa_comp-name,
<l_fld>.
endloop.
uline.
endloop.
运行结果如下: