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 multiple lines from a Internal Table

Former Member
0 Kudos

Hi,

Is it possible to read multiple lines from an internal table using READ Table syntax.

Read statement suggest to select only one row.

My requirement is that I am looping around an internal table it1. Inside the loop I am looping once again over another table it2 since it2 can contain n entries for a particular value in it1.

Is thereway to avoid this nested looping.

Thanks.

Regards,

Sudharshan N A

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor

Hello Sudharsan,

In some cases it may be impossible to do away with Nested Loops. In these cases, you need to reduce the ABAP processing time efficiently using parallel processing technique:


DATA: LV_INDX TYPE I.

SORT IT1 BY F1. "Here F1 is the common field

SORT IT2 BY F1.

LOOP AT IT1.

* Get the table index from which to loop
  READ TABLE IT2 TRANSPORTING NO FIELDS
  WITH KEY F1 = IT1-F1 BINARY SEARACH.
  IF SY-SUBRC = 0.
    LV_INDX = SY-TABIX.

    LOOP AT IT2 FROM LV_INDX.
      IF IT2-F1 NE IT1-F1. "If the value of F1 changes exit the loop
        EXIT.
      ENDIF.
      " Do your processing for IT2 here
    ENDLOOP.
  ENDIF.

May be other SDNers have some better responses.

Hope this helps.

BR,

Suhas

4 REPLIES 4

former_member209217
Active Contributor

READ statement will pick the first entry encountered in the internal table as per the match criteria

Since, you are having N entries associated with a particular one ,READ statement wont work correctly in this case.

So, youhave to use NESTED LOOP any wayz.

Regards,

Lakshman.

Former Member
0 Kudos

Hi,

Read statement would read only a single line at a time.

Why dont you use a key value to read from your internal table within the loop... instead of nested looping if it is possible

Thanks,

Harini

SuhaSaha
Advisor
Advisor

Hello Sudharsan,

In some cases it may be impossible to do away with Nested Loops. In these cases, you need to reduce the ABAP processing time efficiently using parallel processing technique:


DATA: LV_INDX TYPE I.

SORT IT1 BY F1. "Here F1 is the common field

SORT IT2 BY F1.

LOOP AT IT1.

* Get the table index from which to loop
  READ TABLE IT2 TRANSPORTING NO FIELDS
  WITH KEY F1 = IT1-F1 BINARY SEARACH.
  IF SY-SUBRC = 0.
    LV_INDX = SY-TABIX.

    LOOP AT IT2 FROM LV_INDX.
      IF IT2-F1 NE IT1-F1. "If the value of F1 changes exit the loop
        EXIT.
      ENDIF.
      " Do your processing for IT2 here
    ENDLOOP.
  ENDIF.

May be other SDNers have some better responses.

Hope this helps.

BR,

Suhas

Former Member
0 Kudos

Hi,

It does seem possible to read multiple lines in a internal table using the READ statement with consecutive index. Have a look Rob's reply in this thread though not sure how much performace effective it is compared to nested loops.

Vikranth