READ TABLE with dynamic key fields?

I have the name of a table DATA lv_tablename TYPE tabname VALUE 'xxxxx', and a generic FIELD-SYMBOLS: <lt_table> TYPE ANY TABLE. which contains entries selected from that corresponding table.

I've defined my line structure FIELD-SYMBOLS: <ls_line> TYPE ANY. which i'd use for reading from the table.

Is there a way to create a READ statement on <lt_table> fully specifying the key fields?

I am aware of the statement / addition READ TABLE xxxx WITH KEY (lv_field_name) = 'asdf'., but this however wouldn't work (afaik) for a dynamic number of key fields, and I wouldn't like to create a large number of READ TABLE statements with an increasing number of key field specifications.

Can this be done?


  • Actually i found this to work

    DATA lt_bseg TYPE TABLE OF bseg.
    DATA ls_bseg TYPE bseg.
    DATA lv_string1 TYPE string.
    DATA lv_string2 TYPE string.
    lv_string1 = `    `.
    lv_string2 = lv_string1.
    SELECT whatever FROM wherever INTO TABLE lt_bseg.
    READ TABLE lt_bseg INTO ls_bseg
        WITH  KEY ('MANDT') = 800
                  ('  ')    = ''
                  ('BUKRS') = '0005'
                  ('BELNR') = '0100000000'
                  ('GJAHR') = 2005
                  ('BUZEI') = '002'
                  ('')      = ''
                  ('     ') = ''
                  ('    ') = '         '
                  (lv_string1) = '1'
                  (lv_string2) = ''.

    By using this syntax one can just specify as many key fields as required. If some fields will be empty, then these will just get ignored, even if values are specified for these empty fields.

    One must pay attention that using this exact syntax (static definitions), 2 fields with the exact same name (even blank names) will not be allowed.

    As shown with the variables lv_string1 and lv_string2, at run-time this is no problem.

    And lastly, one can specify the fields in any order (i don't know what performance benefits or penalties one might get while using this syntax)