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

Aletrnatives for Nested Loop to same table

Former Member
0 Likes
3,035

Hi,

Is there any alternative for this logic below.....

Table1 consist of date field. Date field in a particular record is checked with all other previous record and then some logic is proccessed.

Normal way is this...

Loop at table1.

     l_index = sy-tabix.

     Loop at table2(copy of table1) untill l_index.

          some logic.

     Endloop.

Endloop.

Thanks....

1 ACCEPTED SOLUTION
Read only

uppu_narayan
Active Participant
0 Likes
1,883

hi Pradosh,

      According to me your requirement doesn't require a parallel cursor technique.......the logic you have written is perfect to your requirement.....

thanks and regards,

narayan

9 REPLIES 9
Read only

Former Member
0 Likes
1,883

Hi Pradosh,

The above code is a good technique when performance is taken into consideration and u can proceed with it. Called Parallel cursor technique and it is an alternative for Nested Loops.

Could u plz tell if u faced any issue with this one ......

Thanks

Vivek

Read only

0 Likes
1,883

This message was moderated.

Read only

Former Member
0 Likes
1,883

hi,

please find exact code as below

data:lv_index type sy-tabix.

Loop at table1.

     Loop at table2(copy of table1) from lv_index.

      if table1-field <> table2-field

         l_index = sy-tabix.

         exit.

     endif.

          some logic.


       Endloop.

Endloop.

Read only

0 Likes
1,883

Hi Krishna,

Thanks for u r concern.........Your approach is correct but my scenario is different.

I need to compare the date field of the table2 with all other previous record's date field of table1

and put into another table(result, I need).

I need alternative other than Nested Loop.....

or else I have to implement this itself...

Regards

Read only

former_member189845
Active Participant
0 Likes
1,883
Hi Apta,
This is the More sophisticated loop in which parallel cursors .
For EX:
* Entries: 100 (ITAB1), 1000 (ITAB2)
* Line width: 100
* Both tables sorted by key K
I = 1.
LOOP AT ITAB1 INTO WA1.
  LOOP AT ITAB2 INTO WA2 FROM I.
    IF WA2-K <> WA1-K.
      I = SY-TABIX.
      EXIT.
    ENDIF.
    " ...
  ENDLOOP.
ENDLOOP.
this is most efficeint way of coding and measure runtime is only 183 micro seconds.
Thanks,
Siva
Read only

uppu_narayan
Active Participant
0 Likes
1,884

hi Pradosh,

      According to me your requirement doesn't require a parallel cursor technique.......the logic you have written is perfect to your requirement.....

thanks and regards,

narayan

Read only

Former Member
0 Likes
1,883

Hi Pradosh,

Suppose TABLE2 is copy of TABLE1 and you have 1 additional field in table2 called  ZINDEX of TYPE SY-TABIX.

Also assuming that number of data in both internal tables are same (since table2 is reflecting table1):

DATA: ITAB1 TYPE STANDARD TABLE OF TABLE1,

           ITAB2 TYPE STANDARD TABLE OF TABLE2,

           WA1 TYPE TABLE1,

           WA2 TYPE TABLE1.

DATA: INDEX_COUNT TYPE SY-TABIX.     

LOOP AT ITAB1 INTO WA1.

INDEX_COUNT = SY-TABIX.

WA2-ZINDEX = INDEX_COUNT.

MODIFY ITAB2 INDEX  INDEX_COUNT  FROM WA2 TRANSPORTING ZINDEX.

READ TABLE ITAB2 WITH KEY ZINDEX = INDEX_COUNT

IF SY-SUBRC = 0.

perform logic and changes in itab_2

ENDIF.

ENDLOOP.

*******************************

So when SY-TABIX of ITAB1 is 1, changes are made in ITAB2 at index 1, when SY-TABIX of ITAB2 is 2, logic for ITAB2 is executed at index 2 and so on.. And so logic will be applied on each line of ITAB2 till the current index.

(let me know if i am wrong).

Regards,

Khushboo.

Read only

0 Likes
1,883

Thanks for your concern...

You need to understand my requirement....again

Your logic is perfect but it doesn't meet my requirement..

Regards

Read only

Former Member
0 Likes
1,883

hi

Please Check below link and find your solution

http://wiki.sdn.sap.com/wiki/display/Snippets/ABAP+Code+for+Parallel+Cursor+-+Loop+Processing

Thanks & Regards

RKarmakar