1

Let us say I have the following code:

iob_typedescr ?= cl_abap_typedescr=>describe_by_name( 'HOUSES' ).
iob_structdescr_table ?= iob_typedescr.
it_ddic_all  = iob_structdescr_table->get_ddic_field_list( ).

LOOP AT it_ddic_all INTO is_ddic WHERE keyflag EQ 'X'.

  APPEND is_ddic TO it_keyfields.
ENDLOOP.

So basically, in the above code I know all of the key fields in the table HOUSES and they are in the table it_keyfields. Now if I had an internal table which had the same structure as HOUSES and wanted to select data from it how would I be able to do this dynamically? If I knew I had only one key then I could do a READ TABLE as follows:

READ TABLE it_internal_table WITH KEY (key_name) = provided_value TRANSPORTING NO FIELDS.

But in this case I may have more than one key in it_keyfields so I am not sure how I could write the READ TABLE statement for this.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Ikim SS
  • 139
  • 2
  • 8
  • 1
    Just check the [ABAP documentation of READ TABLE - free key](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapread_table_free.htm). It clearly states the valid grammar: `WITH KEY { comp1 = operand1 comp2 = operand2 ... [BINARY SEARCH] }` with comp1/comp2 being `{ comp_name[-sub_comp][{+off(len)}|{->attr}] } | { (name) }`. – Sandra Rossi Aug 27 '22 at 07:54
  • 3
    Does this answer your question? [READ TABLE with dynamic key fields?](https://stackoverflow.com/questions/18869119/read-table-with-dynamic-key-fields) – Sandra Rossi Aug 28 '22 at 08:36

2 Answers2

2

You could create a dynamic where condition and use it with LOOP WHERE. Here is a snippet using table SPFLI as an example.

" variable that contains dynamic where condition later
DATA lv_where TYPE string.
" field-symbol that can contain any data
FIELD-SYMBOLS: <f_field_value> TYPE any.
" define it_internal_table (can be dynamic)
DATA it_internal_table TYPE TABLE OF spfli.
it_internal_table = VALUE #(
    ( mandt = '001' carrid = '002' connid = '0003' cityfrom = 'PARIS' )
    ( mandt = '001' carrid = '004' connid = '0005' cityfrom = 'BERLIN' )
).

" define provided values that need to be searched in it_internal_table
DATA(ls_provided_entry) = VALUE spfli( mandt = '001' carrid = '002' connid = '0003' cityfrom = 'PARIS' ).

" create dynamic where condition
LOOP AT it_keyfields ASSIGNING FIELD-SYMBOL(<f_key>).
    ASSIGN COMPONENT <f_key>-fieldname OF STRUCTURE ls_provided_entry TO <f_field_value>. 
    IF sy-subrc = 0.
        IF lv_where IS INITIAL.
            lv_where = |{ <f_key>-fieldname } = `{ <f_field_value> }`|.
        ELSE.
            DATA(lv_where_and) = |AND { <f_key>-fieldname } = `{ <f_field_value> }`|.
            CONCATENATE lv_where lv_where_and INTO lv_where SEPARATED BY space.
        ENDIF.
    ENDIF.
ENDLOOP.

" now check with LOOP WHERE if the provided entry exists in it_internal_table
LOOP AT it_internal_table ASSIGNING FIELD-SYMBOL(<f_internal_table_line>) WHERE (lv_where).
    " if this point is reached, the entry exists;
    " <f_internal_table_line> contains the complete line
    WRITE 'The entry exists'.
    EXIT.
ENDLOOP.
Stefan
  • 241
  • 5
-1

If you want to handle different numbers of key fields dynamically at runtime then you won't be able to do that with a READ command. However, you can now also read data from an internal table using SELECT and there you can dynamically create a WHERE condition that selects on fields only known during runtime.

From the documentation: Use of SELECT to access an internal table using as an alternative to the statement READ TABLE. Unlike READ TABLE, the statement SELECT offers a (dynamic) WHERE condition and evaluates the field list for the inline declaration. The statement is executed on the AS ABAP and the data in the internal table is not transported to the database.

Gert Beukema
  • 2,510
  • 1
  • 17
  • 18
  • 1
    I think you linked to the wrong documentation article. It does not contain the text you quoted. I think the one you meant to link to is [this one](https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapselect_itab.htm)? – Philipp Aug 28 '22 at 17:13
  • Further, this answer could really need an example which demonstrates how to use SELECT on an internal table where the type of the table isn't known at activation-time. The standard example does it with a table where it *is* known, which is significantly easier than what was requested in the question. – Philipp Aug 28 '22 at 17:17