Application Development 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 two dynamic internal tables with dynamic key and also values assinged

UmaArjunan
Active Participant
0 Kudos

Hi Experts,

I have two dynamic internal tables. first will have the old values and the second will have new values of same structure

All dynamic tables and work areas are created, assigned and populated with values before this step

My question can we make the read statement with dynamic with key option and dynamic value populated option.

Based on the first dynamic internal table , workarea values in the second dynamic internal table are read . Both internal table are having the same structure

My code looks like as below :

data: lv_string type string.

loop at <dyn_table_x> into <dyn_wa_x>

dynamic_field = p_table. " fileld name is determined with the user input

"i.e it is the key field in dyn_table_x and dyn_Table_y

read table <dyn_table_y> into <dyn_wa_y>

with key (dynamic_field) = <dyn_wa_x>-<fieldname> filled based on the outer loop.

  • similarly the value of <fieldname> is same as that of p_table. But based on the user input the value might get change everytime

endloop.

What change needs to be done in the above read statement in order to read fieldname and its values dynamically ?

i have checked SDN for the read statment :

from sdn :

READ TABLE <fs_data> ASSIGNING <fs_data_aux> WITH KEY (field_key) = 'TEST'.

In this read i want to pass dynamic variable name instead of value 'TEST'. Can we acheive this ?

Thanks,

ABAPer

1 ACCEPTED SOLUTION

MarcinPciak
Active Contributor

Like this


field-symbols <comp> type any.

assign component 'FIELDNAME' of structure <dyn_wa_x> to <comp>.
"now you can read it
read table <dyn_table_y> into <dyn_wa_y> 
with key (dynamic_field) = <comp>.

the 'FIELDNAME' can be substituted with variable that holds name of the component.

Regards

Marcin

4 REPLIES 4

MarcinPciak
Active Contributor

Like this


field-symbols <comp> type any.

assign component 'FIELDNAME' of structure <dyn_wa_x> to <comp>.
"now you can read it
read table <dyn_table_y> into <dyn_wa_y> 
with key (dynamic_field) = <comp>.

the 'FIELDNAME' can be substituted with variable that holds name of the component.

Regards

Marcin

0 Kudos

Thanks Marcin and Keshav for your swift response.

As per your suggestion , i will change the code to

loop at <dyn_table_x> into <dyn_wa_x>.

Assign component p_table of structure <dyn_wa_x>

to <comp>

read table <dyn_table_y> into <dyn_wa_y> with key

(dynamic_field) = <comp>.

endloop.

and check it .

.

I have a small clarification : but if i use assign component inside my loop endloop the component would be assinged for every read of the record. is that fine ?

Thanks,

Abap er

0 Kudos

I have a small clarification : but if i use assign component inside my loop endloop the component would be assinged for every read of the record. is that fine ?

It must be there, as in each loop <dyn_wa_x> is changing, hence <comp> will also have to change each time.

Regards

Marcin

kesavadas_thekkillath
Active Contributor
0 Kudos

This syntax will work but you have to create the dyn table and dynwork area properly


loop at <dyn_table_x> assigning <dyn_wa_x>
read table <dyn_table_y> assigning <dyn_wa_y> with key (p_table) = <dyn_wa_x>-<fieldname> .
if sy-subrc = 0.
...
endif.
endloop.

@marcin - Hi .. Do we need a assign component when its getting assigned in the loop ?