How to set dynamic key for READ TABLE?
Asked Answered
F

5

8

I'm trying to work out a way to read an internal table that has to be created dynamically. I have created the following report that fills a dynamic internal table with data.

On the last line, I'm trying to read it with a key (mandt for example), but I I get this syntax error:

The specified type has no structure and therefore no component called MANDT

I have debugged and I can see that <any_tab> has been populated successfully and the structure of the table (field names) are correct. The problem presents itself when I try to read the table into a work area. Maybe I'm doing this wrong, but it seems like something that should be possible to do, and I have a feeling I'm missing something small.

The reason I am trying this out is that I have found identical selects happening in a program and want to buffer records in memory and read them from there to avoid DB accesses. This is easy to implement, however I haven't done this when the table, where clause and into clause of the OPEN SQL statement I'm trying to optimize are dynamic.

How to correct the syntax error?

DATA: t681_rep TYPE TABLE OF t681 , wa_681 LIKE LINE OF t681_rep,
      tabref TYPE REF TO data , waref TYPE REF TO data.

FIELD-SYMBOLS: <any_tab> TYPE ANY TABLE,
               <any_wa> TYPE ANY,
               <var1> TYPE ANY.
"fill t681_rep
SELECT *
  FROM t681
  INTO TABLE t681_rep
   UP TO 1 ROWS WHERE kotab = 'A002'.

READ TABLE t681_rep INTO wa_681 WITH KEY kotab = 'A002'.
IF sy-subrc = 0.

  "if A002 is found create a table of that type and fill it
  CREATE DATA tabref TYPE TABLE OF (wa_681-kotab).
  ASSIGN tabref->* TO <any_tab>.
  SELECT * UP TO 10 ROWS
    FROM (wa_681-kotab)
    INTO TABLE <any_tab>.

ENDIF.

CREATE DATA waref TYPE a002.
ASSIGN waref->* TO <any_wa>.

READ TABLE <any_tab> ASSIGNING <any_wa> WITH KEY mandt = '800'. <- problem area
IF sy-subrc = 0.
  "do stuff with <any_wa>...
ENDIF.
Falcate answered 29/5, 2012 at 15:39 Comment(0)
I
5

You just need to put the field name in parentheses.

data: field type string.
field = 'MANDT'.
READ TABLE <any_tab> ASSIGNING <any_wa> WITH KEY (field) = '800'. 
IF sy-subrc = 0.
  "do stuff with <any_wa>...
ENDIF.
Interweave answered 29/5, 2012 at 17:53 Comment(3)
Hi Brian, thanks for your reply. That does work, however I am looking for a fully dynamic solution. In pseudocode something like:Falcate
It was probably a comment referring to the problem of unknown number of key fields. If you use your proposed syntax, you need to know how many key fields a table has in advance.Occasion
@Occasion Probably. When I posted that comment seven years ago I figured I’d try to after the OP corrected their response. When they never did, I forgot all about it.Interweave
A
2

AFAIK, you have to do it the 'long way round':

FIELD-SYMBOLS: <any_field> TYPE any.    
LOOP AT <any_tab> ASSIGNING <any_wa>.
  ASSIGN COMPONENT 'MANDT' OF STRUCTURE <any_wa> TO <any_field>.
  IF <any_field> <> 800.
   CONTINUE.
  ENDIF.
  " do stuff with <any_wa> - you will have to assign <any_field> again to access fields.
ENDLOOP.
Awhirl answered 29/5, 2012 at 15:54 Comment(0)
S
2

You are trying to beat a database in efficiency, it is a loosing battle.

Just go to SE11, select your table, go to technical settings and change the technical settings ( buffering & buffering type ), you do not require an object modification key for this. You can also make sure that the size category is correct.

Stare answered 29/5, 2012 at 20:24 Comment(0)
S
1

You can use RTTS to get the table keys.

data table_name type string.
table_name = 'A002'.

" Dynamically create the table type
data the_table type ref to data.
create data the_table type table of (table_name).

" Use RTTS to get table keys
data typedescription type ref to cl_abap_tabledescr.
typedescription ?= cl_abap_tabledescr=>describe_by_data_ref( the_table ).
data keys type abap_table_keydescr_tab.
keys = typedescription->get_keys( ).
Speculate answered 31/5, 2012 at 13:19 Comment(0)
M
0
REPORT  y_test_dynamic_table.
DATA: table_name TYPE string,
 typedescription TYPE REF TO cl_abap_tabledescr,
 keys TYPE abap_keydescr_tab,
 ls_key TYPE abap_keyname.

table_name = 'ZYFRM_STG'.

" Dynamically create the table type
DATA the_table TYPE REF TO data.
CREATE DATA the_table TYPE TABLE OF (table_name).

" Use RTTS to get table keys

typedescription ?= cl_abap_tabledescr=>describe_by_data_ref( the_table ).

keys = typedescription->KEY.

loop at keys   INTO ls_key.
***
ENDLOOP.
Mapel answered 10/9, 2013 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.