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

removing loop from loop.

Former Member
0 Likes
998

Can any one help me on this issue,

i have to remove the loop on bseg ans so that i can add .

CLEAR : w_debit ,

w_credit .

LOOP AT wtl_temp ASSIGNING <fs>.

READ TABLE wtl_prctr WITH KEY bukrs = <fs>-bukrs

belnr = <lfs>-belnr

gjahr = <lfs>-gjahr

prctr = <lfs>-prctr

hkont = <lfs>-hkont

TRANSPORTING NO FIELDS.

IF sy-subrc NE 0.

MOVE : <lfs1>-bukrs TO wsl_prctr-bukrs,

<fs>-belnr TO wsl_prctr-belnr,

<fs>-gjahr TO wsl_prctr-gjahr,

<fs>-prctr TO wsl_prctr-prctr,

<fs>-hkont TO wsl_prctr-hkont.

LOOP AT wt_bseg ASSIGNING <fs2> WHERE bukrs EQ <fs>-bukrs

AND belnr EQ <fs>-belnr

AND gjahr EQ <fs>-gjahr

AND prctr EQ <fs>-prctr

AND hkont EQ <fs>-hkont.

CASE <fs2>-shkzg.

WHEN c_debit .

w_debit = w_debit + <fs2>-dmbtr.

WHEN c_credit.

w_credit = w_credit + <fs2>-dmbtr.

ENDCASE.

ENDLOOP.

wsl_prctr-dmbtr = w_credit - w_debit.

APPEND wsl_prctr TO wtl_prctr.

CLEAR : wsl_prctr ,

w_debit ,

w_credit .

ELSE.

CONTINUE.

ENDIF.

ENDLOOP.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
962

Hi

avoid loop on that table and put

READ TABLE wt_bseg with key bukrs EQ <fs>-bukrs

this will improve ur performnce also

8 REPLIES 8
Read only

Former Member
0 Likes
963

Hi

avoid loop on that table and put

READ TABLE wt_bseg with key bukrs EQ <fs>-bukrs

this will improve ur performnce also

Read only

0 Likes
962

hi naresh,

as u told it is correct,but i have lot many records with same belnr in the bseg.

Read only

0 Likes
962

Hi,

u can use parallel Cursor method here.

if it is compulsory to use nested loops, then we need to go for PARALLEL CURSOR METHOD

this is very efficient method. This decreases the execution time and increases the performance.

here is a sample code for PARALLEL CURSOR METHOD

Nested Loops – This is one of the fear factors for all the ABAP developers as this consumes lot of program execution time. If the number of entries in the internal tables is huge, then the situation would be too worse. The solution for this is to use parallel cursor method whenever there is a need for Nested Loop.

Program using Normal Nested Loop:

REPORT ZNORMAL_NESTEDLOOP.

TABLES:

likp,

lips.

Data:

t_likp type table of likp,

t_lips type TABLE OF lips.

data:

W_RUNTIME1 TYPE I,

W_RUNTIME2 TYPE I.

START-OF-SELECTION.

select *

from likp

into table t_likp.

select *

from lips

into table t_lips.

get RUN TIME FIELD w_runtime1.

loop at t_likp into likp.

loop at t_lips into lips where vbeln eq likp-vbeln.

endloop.

endloop.

get RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.

write w_runtime2.

Nested Loop using Parallel Cursor:

REPORT zparallel_cursor2.

TABLES:

likp,

lips.

DATA:

t_likp TYPE TABLE OF likp,

t_lips TYPE TABLE OF lips.

DATA:

w_runtime1 TYPE i,

w_runtime2 TYPE i,

w_index LIKE sy-index.

START-OF-SELECTION.

SELECT *

FROM likp

INTO TABLE t_likp.

SELECT *

FROM lips

INTO TABLE t_lips.

GET RUN TIME FIELD w_runtime1.

SORT t_likp BY vbeln.

SORT t_lips BY vbeln.

LOOP AT t_likp INTO likp.

LOOP AT t_lips INTO lips FROM w_index.

IF likp-vbeln NE lips-vbeln.

w_index = sy-tabix.

EXIT.

ENDIF.

ENDLOOP.

ENDLOOP.

GET RUN TIME FIELD w_runtime2.

w_runtime2 = w_runtime2 - w_runtime1.

WRITE w_runtime2.

Analysis report: Runtime in microseconds:

Iteration No ....._Normal Nest Loop_ ..... Using Parallel Cursor

1 ....................... 34,796,147 ................... 63,829

2 .........................38,534,583 ................... 56,894

3 .........................34,103,426 ................... 50,510

u can check this site for more details

http://www.saptechnical.com/Tutorials/ABAP/ParallelCursor.htm

reward if helpful

raam

Read only

Former Member
0 Likes
962

Hi

Another choice could be defining your internal table as SORTED.

A loop with specified key on a sorted table should loop only on the relevant part of the table.

Just change table definition:

DATA: wt_bseg TYPE SORTED TABLE OF BSEG WITH NON-UNIQUE KEY

BUKRS BELNR GJHAR PRCTR HKONT.

You should not modify your loop.

If you select from BSEG into wt_bseg with "INTO TABLE" option you should not modify your code.

If you select from BSEG into wt_bseg with "APPEND" statement, you should replace your

"APPEND <WA> TO WT_BSEG" code with "INSERT <WA> INTO TABLE WT_BSEG" code

Regards

Marco

Read only

Former Member
0 Likes
962

try looping on item table and update the values from header table by read statement.

Even if there are multiple line items in item table, all line items will get updated.

Read only

Former Member
0 Likes
962

If you need a loop inside another loop then you should use either

a sorted table for the inner table

or if you need the standard table, then read binary search, loop from index if ( condition not fulfilled ) exit endif.

see last section of

Measurements on internal tables: Reads and Loops:

/people/siegfried.boes/blog/2007/09/12/runtimes-of-reads-and-loops-on-internal-tables

for details.

I would not recommend parallel cursor, the real fault tolerant solution is much more complicated then the reference above.

Siegfried

Read only

Former Member
0 Likes
962

thanks every body

Read only

Former Member
0 Likes
962

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

  • Performance Tuning using parallel cursor

*

  • Extracts from program ZFAL2002

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

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

  • START-OF-SELECTION

SELECT *

INTO TABLE I_KEPH FROM KEPH

WHERE KADKY <= SY-DATUM

AND TVERS = '01'

AND KALKA IN ('01','Z1','Z2')

AND BWVAR IN ('Z01','Z02','Z03','Z04','Z07')

AND KKZST = ' '

AND KKZMA = ' '

AND KKZMM = ' '

AND KEART = 'H'

AND PATNR = 0.

  • Table must be sorted to ensure all required records are together

SORT I_KEPH BY KALNR KALKA BWVAR KADKY.

  • Perform actual processing

Perform get_cost_values.

----


FORM GET_COST_VALUES.

  • Determine start position and then process all records for given key

  • from that starting point

  • i_keph is sorted on kalnr kalka bwvar kadky.

READ TABLE I_KEPH WITH KEY KALNR = W_KEKO-KALNR

KALKA = W_KEKO-KALKA

BWVAR = W_KEKO-BWVAR

KADKY = W_KEKO-KADKY BINARY SEARCH.

IF SY-SUBRC = 0.

  • Loop at itab from first record found (sy-tabix) until record

  • no-longer matches your criteria.

LOOP AT I_KEPH FROM SY-TABIX.

IF I_KEPH-KALNR = W_KEKO-KALNR AND I_KEPH-KALKA = W_KEKO-KALKA

AND I_KEPH-BWVAR = W_KEKO-BWVAR AND I_KEPH-KADKY = W_KEKO-KADKY.

  • Key match

D_MAT_COST = D_MAT_COST + I_KEPH-KST001.

D_LAB_COST = D_LAB_COST + I_KEPH-KST004.

D_OVER_HEAD = D_OVER_HEAD + I_KEPH-KST010.

D_EXT_PURCH = D_EXT_PURCH + I_KEPH-KST014.

D_MISC_COST = D_MISC_COST + I_KEPH-KST002 + I_KEPH-KST003

+ I_KEPH-KST005 + I_KEPH-KST006 + I_KEPH-KST007

+ I_KEPH-KST008 + I_KEPH-KST009 + I_KEPH-KST011

+ I_KEPH-KST012 + I_KEPH-KST013 + I_KEPH-KST015

+ I_KEPH-KST016 + I_KEPH-KST017 + I_KEPH-KST018

+ I_KEPH-KST019 + I_KEPH-KST020 + I_KEPH-KST021

+ I_KEPH-KST022 + I_KEPH-KST023 + I_KEPH-KST024

+ I_KEPH-KST025 + I_KEPH-KST026 + I_KEPH-KST027

+ I_KEPH-KST028 + I_KEPH-KST029 + I_KEPH-KST030

+ I_KEPH-KST031 + I_KEPH-KST032 + I_KEPH-KST033

+ I_KEPH-KST034 + I_KEPH-KST035 + I_KEPH-KST036

+ I_KEPH-KST037 + I_KEPH-KST038 + I_KEPH-KST039

+ I_KEPH-KST040.

ELSE.

  • Key greater - can't be less

EXIT. " Exit loop

ENDIF.

ENDLOOP.

ENDIF.

D_MAT_COST = D_MAT_COST / W_KEKO-LOSGR.

D_LAB_COST = D_LAB_COST / W_KEKO-LOSGR.

D_OVER_HEAD = D_OVER_HEAD / W_KEKO-LOSGR.

D_EXT_PURCH = D_EXT_PURCH / W_KEKO-LOSGR.

D_MISC_COST = D_MISC_COST / W_KEKO-LOSGR.

ENDFORM. " GET_COST_VALUES