Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Read hashed internal table with runtime key

Former Member
0 Likes
9,797

Hi all,

we are trying to obtain this result:

  • We have a source standard table itab
  • We want transform this table to a hashed table
  • The unique key of the hashed table must be calculated at runtime (e.g. the columns of the table used as key are known only at runtime)
  • Then we want to read data from the table using the key defined above.

We are able to convert the standard table to a hashed table but we are not able to define the key at runtime.

We found this: Reading Lines of Tables (SAP Library - ABAP Programming (BC-ABA))

If you do not know the name of one of the key fields until runtime, you can specify it dynamically as the content of a field n1 ... nn using (n1)
= f1 (n2) = f2
If the data types of f1
... fn are not compatible with the key fields, the system converts them. If the row type of the internal table is not structured, you can specify a comparison with the pseudo-component table_line.

But we cannot understand how does it work.

Could you help us?

Thank you

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,539

Hi all,

I found the solution. You need to put all the key columns name on a standard table and include it in brackets (...) as internal table.

Here an example:

       TYPES: BEGIN OF ls_key_elem,
            name(32) TYPE c,
       END OF ls_key_elem.

      DATA: lt_key_elem TYPE STANDARD TABLE OF ls_key_elem,
                  wa_key_elem TYPE ls_key_elem.

      wa_key_elem-name = 'ID'.
      APPEND wa_key_elem TO lt_key_elem.

      wa_key_elem-name = 'ID2'.
      APPEND wa_key_elem TO lt_key_elem.

      CREATE DATA lt_table LIKE HASHED TABLE OF <ls_table> WITH UNIQUE KEY (lt_key_elem).
      ASSIGN lt_table->* to <lt_table>.

      READ TABLE <lt_table> ASSIGNING <ls_table> WITH TABLE KEY ('ID') = 'IDVALUE' ('ID2') = 'ID2VALUE'.


Thank you

4 REPLIES 4
Read only

Former Member
0 Likes
3,539

Hi,

If you want to convert standard table into hashed table as part performance then  you can try with secondary keys..

Like,

DATA : t_mara TYPE STANDARD TABLE OF mara

            WITH UNIQUE HASHED KEY sec_key COMPONENTS matnr.

READ TABLE t_table INTO wa_mara WITH TABLE KEY sec_key COMPONENTS matnr = matnr.

Thanks & Regards,

Arun

Read only

0 Likes
3,539

Hi Arun,

thank you for your answer.

This could be useful but we still do not know how to define the key at runtime.

In your example matnr is just a component. How can we define these components at runtime?

Read only

0 Likes
3,539

Hi Alberto,

Are you creating the hash table dynamically ? source table of  type also dynamic ?

Thanks & Regards,

Arun

Read only

Former Member
0 Likes
3,540

Hi all,

I found the solution. You need to put all the key columns name on a standard table and include it in brackets (...) as internal table.

Here an example:

       TYPES: BEGIN OF ls_key_elem,
            name(32) TYPE c,
       END OF ls_key_elem.

      DATA: lt_key_elem TYPE STANDARD TABLE OF ls_key_elem,
                  wa_key_elem TYPE ls_key_elem.

      wa_key_elem-name = 'ID'.
      APPEND wa_key_elem TO lt_key_elem.

      wa_key_elem-name = 'ID2'.
      APPEND wa_key_elem TO lt_key_elem.

      CREATE DATA lt_table LIKE HASHED TABLE OF <ls_table> WITH UNIQUE KEY (lt_key_elem).
      ASSIGN lt_table->* to <lt_table>.

      READ TABLE <lt_table> ASSIGNING <ls_table> WITH TABLE KEY ('ID') = 'IDVALUE' ('ID2') = 'ID2VALUE'.


Thank you