简体   繁体   中英

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.

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. In theoriginal article I read, the author used them with a ABAP Dictionary table, my goal is to use it with arbitrary internal tables.

I tried it within a test report and used T005 for the test:

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. Since it is no ABAP Dictionary Table this kinda makes sense to me. 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 .

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.

Here is a good overview of all RTTS objects and their methods available.

You are trying to create a table description using the class 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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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