‎2007 May 12 2:53 AM
How can i avoid nested loop??
ex...
To fetch item from corresponding header records??
For the above case i can use nested loops but how
2 improve performance??
‎2007 May 12 3:25 AM
Use For all entries or Joins condition, do not use nested loop if possible.
some times we need to use nested loop
‎2007 May 12 4:55 AM
Hi,
To improve performance you can use parallel cursor technique...
Check this Ex..
Let's say ITAB_VBAK is for header and ITAB_VBAP is for line item..
DATA: V_TABIX TYPE SYTABIX.
* Sort the line item with the column that is common to both header and line item.
* In this scenario I assumed VBELN.
SORT ITAB_VBAP BY VBELN.
LOOP AT ITAB_VBAK.
* Get the index
READ TABLE ITAB_VBAP TRANSPORTING NO FIELDS
WITH KEY VBELN = ITAB_VBAK-VBELN
BINARY SEARCH.
IF SY-SUBRC = 0.
* Store the index.
V_TABIX = SY-TABIX.
* Process from that index.
LOOP AT ITAB_VBAP FROM V_TABIX.
* Exit condition.
IF ITAB_VBAP-VBELN <> ITAB_VBAK-VBELN.
EXIT.
ENDIF.
* here you can get the line item records..
ENDLOOP.
ENDIF.
ENDLOOP.
Thanks,
Naren
‎2007 May 12 4:55 AM
Hi,
1 You can use join.
2 Or you can use nested loops by using where clause.
Performance is depends on lot many factors, such as no. of records fetched, no of records in DB table, indexes on the table, etc.
Regards,
Atish
‎2007 May 12 8:24 AM
Hi,
Check this Program
This wil perform better instead of using nested loops.
REPORT zeasproject2_report .
TABLES : anla,
zcust_table_01.
************************************************************************
INTERNAL TABLE AND WORK AREA FOR FIELDS IN ANLA TABLE
************************************************************************
DATA : BEGIN OF itab_anla OCCURS 0,
bukrs LIKE anla-bukrs, "Company Code
anln1 LIKE anla-anln1, "Main Asset Number
anln2 LIKE anla-anln2, "Asset Subnumber
anlkl LIKE anla-anlkl, "Asset class
END OF itab_anla.
DATA : wa_anla LIKE LINE OF itab_anla.
************************************************************************
INTERNAL TABLE AND WORK AREA FOR FIELDS IN ZCUST_TABLE_01 TABLE *
************************************************************************
DATA : BEGIN OF itab_zcust_table_01 OCCURS 0,
bukrs LIKE zcust_table_01-bukrs,
anln1 LIKE zcust_table_01-anln1,
anln2 LIKE zcust_table_01-anln2,
anlkl LIKE zcust_table_01-anlkl,
z001 LIKE zcust_table_01-z001,
z002 LIKE zcust_table_01-z002,
z003 LIKE zcust_table_01-z003,
z004 LIKE zcust_table_01-z004,
END OF itab_zcust_table_01.
DATA : wa_zcust_table_01 LIKE LINE OF itab_zcust_table_01.
************************************************************************
OUTPUT INTERNAL TABLE *
************************************************************************
DATA : BEGIN OF itab_output OCCURS 0,
bukrs LIKE anla-bukrs,
anln1 LIKE anla-anln1,
anln2 LIKE anla-anln2,
anlkl LIKE anla-anlkl,
z001 LIKE zcust_table_01-z001,
z002 LIKE zcust_table_01-z002,
z003 LIKE zcust_table_01-z003,
z004 LIKE zcust_table_01-z004,
END OF itab_output.
***********************************************************************
SELECTION-SCREEN BEGIN OF BLOCK input WITH FRAME TITLE text-t01.
SELECT-OPTIONS : bukrs FOR anla-bukrs,
anln1 FOR anla-anln1,
anln2 FOR anla-anln2,
anlkl FOR anla-anlkl.
SELECTION-SCREEN END OF BLOCK input.
***********************************************************************
SELECTING RECORDS FORM ANLA TABLE *
***********************************************************************
SELECT bukrs anln1 anln2 anlkl
FROM anla INTO CORRESPONDING FIELDS OF TABLE itab_anla
WHERE bukrs IN bukrs AND
anln1 IN anln1 AND
anln2 IN anln2 AND
anlkl IN anlkl.
***********************************************************************
SELECT bukrs anln1 anln2 anlkl z001 z002 z003 z004
FROM zcust_table_01 INTO CORRESPONDING FIELDS OF
TABLE itab_zcust_table_01.
**********************************************************************
LOOP AT itab_anla.
READ TABLE itab_zcust_table_01 WITH KEY
bukrs = itab_anla-bukrs
anln1 = itab_anla-anln1
anln2 = itab_anla-anln2
anlkl = itab_anla-anlkl BINARY SEARCH.
IF sy-subrc EQ 0.
MOVE-CORRESPONDING itab_anla TO itab_output.
MOVE-CORRESPONDING itab_zcust_table_01 TO itab_output.
APPEND itab_output.
ENDIF.
ENDLOOP.
***********************************************************************
ULINE 1(106).
FORMAT HOTSPOT ON.
FORMAT COLOR 6 INVERSE.
LOOP AT itab_output.
WRITE :/001 sy-vline,
006 itab_output-bukrs, 15 sy-vline,
021 itab_output-anln1, sy-vline,
042 itab_output-anln2, 52 sy-vline,
057 itab_output-anlkl, 65 sy-vline,
068 itab_output-z001 , 75 sy-vline,
078 itab_output-z002 , 84 sy-vline,
088 itab_output-z003 , 93 sy-vline,
098 itab_output-z004 ,
106 sy-vline.
ENDLOOP.
FORMAT HOTSPOT OFF.
ULINE 1(106).
************************************************************************
TOP-OF-PAGE.
ULINE 1(106).
FORMAT HOTSPOT ON.
FORMAT COLOR 2 INVERSE.
WRITE :/001 sy-vline,
002 'COMPANY CODE' , sy-vline,
016 'MAIN ASSET NUMBER', sy-vline,
036 'ASSET SUBNUMBER' , sy-vline,
053 'ASSET CLASS' , sy-vline,
069 'Z001' , 75 sy-vline,
078 'Z002' , 84 sy-vline,
087 'Z003' , 93 sy-vline,
097 'Z004',
106 sy-vline.
FORMAT HOTSPOT OFF.
************************************************************************
Reward if it useful to you.
‎2007 May 15 12:06 PM
hi praveen
A simple SELECT loop is a single database access whose result is passed to the ABAP program line by line. Nested SELECT loops mean that the number of accesses in the inner loop is multiplied by the number of accesses in the outer loop. You should therefore only use nested SELECT loops if the selection in the outer loop contains very few lines.
However, using combinations of data from different database tables is more the rule than the exception in the relational data model. You can use the following techniques to avoid nested SELECT statements.
To avoid nested select statements we use SELECT FOR ALL ENTRIES statement. ...
REWARD IT PLEASE...!!
‎2007 May 15 12:25 PM
Hi,
Avoid nested looping on the internal tables they eat up processor time.
Eg:
Loop at I_vbak
Read table I_vbap with key vbeln eq i_vbak-vbeln transporting with no field.
loop at i_vbap.
if Condition.
endif.
endloop
endloop.
Regards
Ganesh
‎2007 May 15 12:42 PM
You should avoid nested loops, as they are really inefficient. You may do it like this:
SORT itab BY kunnr.
LOOP AT itab INTO w_tab.
READ TABLE itab2 INTO w_tab2 WITH KEY kunnr = w_tab-kunnr
BINARY SEARCH.
itab_index = sy-tabix.
WHILE sy-subrc = 0.
itab_index = itab_index + 1.
READ TABLE itab2 INTO w_tab2 INDEX itab_index.
if w_tab-kunnr <> w_tab2-kunnr.
sy-subrc = 99.
else.
endif.
endwhile.
ENDLOOP.Please reward if helpful!