Can a READ TABLE statement be created dynamically

2019-07-19 08:33发布

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?

标签: sql crud abap
2条回答
男人必须洒脱
2楼-- · 2019-07-19 09:21

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)

查看更多
Evening l夕情丶
3楼-- · 2019-07-19 09:23

There seems to be the possibility ( like a dynamic select statement whith binding and lt_dynwhere ).

Please refer to this post, there was someone, who also asked for the requirement:

http://scn.sap.com/thread/1789520

查看更多
登录 后发表回答