Search code examples
abaprtts

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 the original 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


Solution

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