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

loop V.S read

Former Member
0 Likes
1,354

Hi All,

I have a question here (kinda stupid)...I wrote two codes..both return me same result.

But I have to know which way better....The Loop V.S The Read.

Below are my codes:

The Loop way........

LOOP AT it_table WHERE mtart = 'ZTG' OR mtart = 'ZPM'.
  EXIT.
ENDLOOP.

IF sy-subrc = 0.
  READ TABLE it_table WITH KEY mtart = 'ZSM' BINARY SEARCH.
  IF sy-subrc = 0.
    lv_flag = 1.           
  ELSE.
    lv_flag = 0.           
  ENDIF.
ELSE.
  lv_flag = 0.          
ENDIF.

and the Read way.....

READ TABLE it_table WITH KEY mtart = 'ZTG' BINARY SEARCH.
IF sy-subrc NE 0.
  READ TABLE it_table WITH KEY mtart = 'ZPM' BINARY SEARCH.
  IF sy-subrc = 0.
    lv_flag1 = 0.       
  ELSE.
    lv_flag1 = 1.        
  ENDIF.
ELSE.
  lv_flag1 = 0.        
ENDIF.

READ TABLE it_table WITH KEY mtart = 'ZSM' BINARY SEARCH.
IF sy-subrc = 0.
  IF lv_flag1 = 0.          
    lv_flag = 1.            
  ELSE.                    
    lv_flag = 0.           
  ENDIF.
ELSE.
  lv_flag = 0.           
ENDIF.

Can someone please tell me which way better?

The fastest and most effective.

I tried SE30, and I got different duration every time I run above codes.

I tried ST05, I can't get what I want....the duration of these codes...

Please advice and help.

Thanks.

Cheers,

Law

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
803

Hi,

Read statement with binary search is the best option...

But make sure that ur funtionality is fine....

If both the codes work in the same way , go for the second one...

regards,

lavanya

4 REPLIES 4
Read only

Former Member
0 Likes
804

Hi,

Read statement with binary search is the best option...

But make sure that ur funtionality is fine....

If both the codes work in the same way , go for the second one...

regards,

lavanya

Read only

0 Likes
803

Hmm...but the 2nd way uses more indicator (lv_flag1) and more lines of codes. Doesn't this affected the performance as well?

By the way....before perform both ways, the internal table was sorted by the field mtart.

Read only

0 Likes
803

Go for the second way...even SAP recommends read table compared to loop at..

Read only

Former Member
0 Likes
803

hi check this..

the main difference between the loop and read..

suppose i had one record for every record there may be so many records ...like a record in the mara-matnr there may be many in the mard table ..in this case we use the loop in the loop..

loop at it_mara .

loop at it_mard where matnr = it_mara-matnr.

it_final[] = it_mard[].

append it_final.

endloop.

endloop.

for every record if there is only one record...the we will use the read statement...in the loop..

loop at it_mara .

read table it_marc with key matnr = it_mara-matnr.

endloop.

regards,

venkat.