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 from internal table while looping thru it

Former Member
0 Likes
596

Hello

Is there any issues if i read a record from an internal table while looping thru it ? for eg,

loop at itab1 into itab1_wa.

read itab1 into itab1_wa1 with key field1 = itab1_wa-field2.

endloop.

thank you

pranati

4 REPLIES 4
Read only

Former Member
0 Likes
570

Hi Pranati,

I dont see any immediate issue, however, can relate to a concern regarding a possible issue.

What you could do is, move the contents of your itab1 into a temporary table itab2 before the loop starts using

REFRESH itab2.
itab2[] = itab1[].

Then in your loop, READ TABLE itab2 instead of itab1.

Cheers,

Aditya

Read only

Former Member
0 Likes
570

Is there any issues if i read a record from an internal table while looping thru it ? for eg,
loop at itab1 into itab1_wa.
read itab1 into itab1_wa1 with key field1 = itab1_wa-field2.
endloop.

=> May be not good in performance:

You may write smthing like


loop at itab1 where itab1-field EQ XYZ.
But for this you need to know XYZ.
endloop.

Otherwise there is no problem.

Thank You

Shital

Read only

Former Member
0 Likes
570

Hi Pranati,

There is no harm in uiing READ inside the LOOP for the same table.

But it depends on the purpose of your logic.

If by any case READ and LOOP are avoided then that approach should be followed.

If you can tell your requirement then it will be easier to guide you on this.

Read only

Former Member
0 Likes
570

loop at itab1 into itab1_wa.

read itab1 into itab1_wa1 with key field1 = itab1_wa-field2.

endloop.

There is no issues with the code except for the values in work area.

when you read and assign values to same work area the value that you got in your work area while looping are gone.

So what you can do is use two work areas one for loop and other for read so that you can retain both values till the endloop line.

Other than this you will have to check for conditions that you use.

Also if you want to have a check on performance sort the table and use binary search.

Regards,

Lalit Mohan Gupta.