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 table statement with dynamic key

Former Member
2,516

Hi All,

I want to read a table for which a key is defined in another z table. So basically I want to achieve the following :-

Ideally we write a read statement like this :-

READ TABLE ITAB WITH KEY MATNR = '2021'.

But I want to read in the following way :-

data : var1 type string.

var1 = 'MATNR = ''2021'''.

READ TABLE ITAB WITH KEY VAR1.

The fields and conditions for the key are defined in a z table so I have to pass them dynamically to the read statement.

Please suggest how I can achieve this.

Thanks in advance,

Archana.

8 REPLIES 8
Read only

Former Member
0 Likes
1,312

u cud try to use use field symbols

field-symbols: <fs_f>.

lv_feld = 'MATNR'.

assign (lv_feld) to <fs_f>.

READ TABLE ITAB WITH KEY <fs_f> = '022'.

Read only

Former Member
1,312

Hi archana,

assing var1 value to work area. and use that work area as key.

Bohra.

Read only

0 Likes
1,312

Hi all,

I have already tried using field symbols but that doesn't work either. Plus the fact that I would not even know the value '2021'. Means the whole condition is to be taken from another table.

I also tried using the work area but it doesnt work. Is there a tested code you have tried ?

Read only

0 Likes
1,312

Hi,

try this...it is the way how we get the field dynamically...

tables: spfli.

<b>parameters</b>: field(10) type c,

value(10) type c.

data:

itab like standard table of spfli,

wa like line of itab.

select * from spfli into table itab.

condense field.

condense value.

read table itab with key <b>(field) = value</b> into wa.

if sy-subrc eq 0.

endif.

Read only

0 Likes
1,312

Hi All,

Actually I have a z table as follows :

RULE CONDITION

MER prodid = '10'

Now in the program I will select this row. I have an internal table which will already have a field 'prodid'. Now I want to read the records which satifies the condition 'prodid = 10'. This condition is only defined in the table and can be any field and any operand and any fixed value.

So i have this condition stored in a variable of string type var 1, where var1 = 'prodid=''10'''.

now I have to read the table with this condition.

READ TABLE ITAB WITH KEY VAR1.

Now this syntax is not accepted, so can anybody please suggest how we can achieve this ? I tries using field symbols as well but nothing worked.

The operand and fixed value are also not known so the whole condition has to be taken from the ztable.

Please suggest.

Thanks in advance,

Archana

Read only

Former Member
0 Likes
1,312

Hi Archana,

You can do this by reading all MATNR values from DB table into an internal table and then looping it. You can give READ statment inside this loop and do further processing.

<b>DATA: BEGIN OF itab1 occurs 0,

matnr TYPE MATNR.

END OF itab1.

SELECT MATNR FROM <TABLE> INTO itab1 WHERE <conditions>.</b>

<b>LOOP AT itab1.

READ TABLE ITAB WITH KEY matnr = itab1-matnr.

.

.

.

.

ENDLOOP.</b>

*Reward points if it helps.

Regards,

Amit

Read only

Former Member
0 Likes
1,312

hi

good

call function 'DDIF_FIELDINFO_GET'

exporting

tabname = v_tabname "<< dynamic table name

langu = sy-langu

tables

dfies_tab = i_dfies

exceptions

not_found = 1

internal_error = 2

others = 3.

if sy-subrc eq 0.

loop at i_dfies where fieldname ne 'MANDT' and keyflag eq 'X'.

assign component i_dfies-position of structure <xtab> to <f3>.

<< Here you get the key field value in the field symbol>>

endloop.

endif.

thanks

mrutyun^

Read only

Former Member
0 Likes
1,312

This approach did not work out so changed the approach.