dynamicabapdynamic-tablesrtts

Read dynamic table by index


I have dynamic table which generated like this:

DATA: lo_struct   TYPE REF TO cl_abap_structdescr,
        lo_element  TYPE REF TO cl_abap_elemdescr,
        lo_new_type TYPE REF TO cl_abap_structdescr,
        lo_new_tab  TYPE REF TO cl_abap_tabledescr,
        lo_data     TYPE REF TO data,
        lt_comp     TYPE cl_abap_structdescr=>component_table,
        ls_comp     LIKE LINE OF lt_comp.


  lo_struct ?= cl_abap_typedescr=>describe_by_name( gv_table_name ). 

  lt_comp  = lo_struct->get_components( ).

* <some additional columns adding here>

  lo_new_type = cl_abap_structdescr=>create( lt_comp ).

  lo_new_tab = cl_abap_tabledescr=>create(
                  p_line_type  = lo_new_type
                  p_table_kind = cl_abap_tabledescr=>tablekind_std
                  p_unique     = abap_false ).

  CREATE DATA lo_data TYPE HANDLE lo_new_type.
  ASSIGN lo_data->* TO <fs_line>. 

  CREATE DATA lo_data TYPE HANDLE lo_new_tab.
  ASSIGN lo_data->* TO <fs_tab>.

This table with data then shows in ALV and after user perform double-click on line I wanna get this line to process later. And problem is implementing this, this line of code of course doesn't work:

READ TABLE <fs_tab> INDEX ps_row_no-row_id ASSIGNING <fs_line>.

How can I solve this?

I've tried:

READ TABLE <fs_tab> INDEX ps_row_no-row_id ASSIGNING <fs_line>. 

Error: 588: For tables of type "HASHED TABLE" or "ANY TABLE" type, explicit and implicit index operations are not allowed. operations. "<FS_TAB>" is of type "ANY TABLE". It is possible that no complement is specified before "<FS_TAB>" "TABLE".

Expected: data in <fs_line>.


Solution

  • If you tell the compiler that the internal table can be a Hashed table, it's normal that the compiler refuses an access by index. As you are talking about ALV, I think ALV supports only Standard tables. If you want to accept any table, you should anyway copy it to a Standard table, so in the end <FS_TAB> would be Standard:

    FIELD-SYMBOLS <fs_tab> TYPE STANDARD TABLE.
    

    Generally speaking, a read by index can be done on Standard and Sorted tables, which are both called "index tables". If you want to accept both categories, you could also use:

    FIELD-SYMBOLS <fs_tab> TYPE INDEX TABLE.
    

    References: