简体   繁体   English

获取 itab 的表结构失败

[英]Getting table structure of itab fails

I'm currently working on a project to extract data into several itabs and save them all into a single excel file on my local pc.我目前正在开展一个项目,将数据提取到多个 itabs 中,并将它们全部保存到本地 PC 上的单个 excel 文件中。

For moving my data into the excel file, I have to loop over the fields of the tabel which seems to be archivable with the cl_abap_structdescr=>describe_by_data and cl_abap_tabledescr=>create function.为了将我的数据移动到 excel 文件中,我必须遍历似乎可以使用cl_abap_structdescr=>describe_by_datacl_abap_tabledescr=>create函数存档的cl_abap_structdescr=>describe_by_data的字段。 In theoriginal article I read, the author used them with a ABAP Dictionary table, my goal is to use it with arbitrary internal tables.在我阅读的原始文章中,作者将它们与 ABAP 字典表一起使用,我的目标是将其与任意内部表一起使用。

I tried it within a test report and used T005 for the test:我在测试报告中尝试过,并使用 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 ).

Neither of both describe_by_name() nor describe_by_data() work, describing by name results in a "NOT_FOUND" exception. describe_by_name()describe_by_data()不起作用,按名称描述会导致“NOT_FOUND”异常。 Since it is no ABAP Dictionary Table this kinda makes sense to me.因为它不是 ABAP 字典表,所以这对我来说很有意义。 Describing by data results in a CX_SY_MOVE_CAST_ERROR telling me that the source type \\CLASS=CL_ABAP_TABLEDESC cannot be converted into "\\CLASS=CL_ABAP_STRUCTDESC .通过数据描述导致CX_SY_MOVE_CAST_ERROR告诉我源类型\\CLASS=CL_ABAP_TABLEDESC无法转换为"\\CLASS=CL_ABAP_STRUCTDESC

Thanks in advance提前致谢

Use this variant:使用这个变体:

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>.

Pay attention to the first line which is different from yours, describe_by_data method accepts flat structure, not an itab.注意第一行与你的不同, describe_by_data方法接受平面结构,而不是 itab。

Here is a good overview of all RTTS objects and their methods available.这里很好地概述了所有 RTTS 对象及其可用的方法。

You are trying to create a table description using the class cl_abap_structdescr .您正在尝试使用类cl_abap_structdescr创建表描述。 Which doesn't work, because that class is for structures, not for tables.这不起作用,因为该类用于结构,而不是用于表。

When you want a table description, use the class cl_abap_tabledescr .如果需要表描述,请使用类cl_abap_tabledescr

tabletype ?= cl_abap_tabledescr=>describe_by_data( lt_t005 ).

When you also need the structure description for a line of said table, you can obtain that through the table description:当您还需要该表中某行的结构描述时,可以通过表描述获取:

tablestructure ?= tabletype->get_table_line_type( ).

Note that the last line will throw a CX_SY_MOVE_CAST_ERROR exception if the internal table has a line type which is not a structure (like a TYPE TABLE OF string ).请注意,如果内部表的行类型不是结构(如TYPE TABLE OF string ),则最后一行将抛出CX_SY_MOVE_CAST_ERROR异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM