[英]Getting table structure of itab fails
我目前正在開展一個項目,將數據提取到多個 itabs 中,並將它們全部保存到本地 PC 上的單個 excel 文件中。
為了將我的數據移動到 excel 文件中,我必須遍歷似乎可以使用cl_abap_structdescr=>describe_by_data
和cl_abap_tabledescr=>create
函數存檔的cl_abap_structdescr=>describe_by_data
的字段。 在我閱讀的原始文章中,作者將它們與 ABAP 字典表一起使用,我的目標是將其與任意內部表一起使用。
我在測試報告中嘗試過,並使用 T005 進行測試:
data:
lt_t005 type standard table of t005,
ls_t005 like line of lt_t005,
tablestructure type ref to cl_abap_structdescr,
tabletype type ref to cl_abap_tabledescr.
*tablestructure ?= cl_abap_structdescr=>describe_by_name( 'lt_t005' ).
tablestructure ?= cl_abap_structdescr=>describe_by_data( lt_t005 ).
tabletype ?= cl_abap_tabledescr=>create( p_line_type = tablestructure ).
describe_by_name()
和describe_by_data()
不起作用,按名稱描述會導致“NOT_FOUND”異常。 因為它不是 ABAP 字典表,所以這對我來說很有意義。 通過數據描述導致CX_SY_MOVE_CAST_ERROR
告訴我源類型\\CLASS=CL_ABAP_TABLEDESC
無法轉換為"\\CLASS=CL_ABAP_STRUCTDESC
。
提前致謝
使用這個變體:
tablestructure ?= cl_abap_structdescr=>describe_by_data( ls_t005 ).
tabletype ?= cl_abap_tabledescr=>create( p_line_type = tablestructure ).
DATA table TYPE REF TO data.
FIELD-SYMBOLS: <tab> TYPE ANY TABLE.
CREATE DATA table TYPE HANDLE tabletype.
ASSIGN table->* TO <tab>.
SELECT *
FROM t005
INTO TABLE <tab>.
注意第一行與你的不同, describe_by_data
方法接受平面結構,而不是 itab。
這里很好地概述了所有 RTTS 對象及其可用的方法。
您正在嘗試使用類cl_abap_structdescr
創建表描述。 這不起作用,因為該類用於結構,而不是用於表。
如果需要表描述,請使用類cl_abap_tabledescr
。
tabletype ?= cl_abap_tabledescr=>describe_by_data( lt_t005 ).
當您還需要該表中某行的結構描述時,可以通過表描述獲取:
tablestructure ?= tabletype->get_table_line_type( ).
請注意,如果內部表的行類型不是結構(如TYPE TABLE OF string
),則最后一行將拋出CX_SY_MOVE_CAST_ERROR
異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.