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

how to avoid select inside loop

Former Member
0 Likes
8,224

Dear Friends,

In one of my programs I am using 3 tables VBRK,VBRP and KONV.I have created one internal table with all fields which is necessary for output..My requirement was to check a particular condition type for (ZVAT) for a bill if it exists I want to select another condition value ( Value of PR00 condition). I selected data from VBRK,VBRP using inner join into an internal table But since it is not possible to select data from Konv using join I started a loop in the first internal table and used a two single select statement inside that loop (One for existence of first condition and second for value if first condition is true).

But I have learned from many sources that select inside a loop also affects the performance of the program like select – endselect.So anybody can suggest an alternate way for avoiding this situation ie. Select inside loop. I have faced similar situations in other programs also. I am attaching code of my program also..

So anybody can check this code and give an alternate solution to avoid select inside the loop and improve performance of my program

SELECT AVBELN AFKART AKNUMV AFKDAT AKUNAG BPOSNR BARKTX BVGBEL INTO TABLE ITAB1

FROM VBRK AS A JOIN VBRP AS B ON AVBELN = BVBELN

WHERE AFKDAT IN DATE AND AFKART NE 'S1' AND BVKBUR IN SOFFICE AND BWERKS IN PLANT.

LOOP AT ITAB1 INTO ITAB1_WA.

SELECT SINGLE KWERT INTO (VATVAL) FROM KONV WHERE KNUMV = ITAB1_WA-KNUMV AND KPOSN = ITAB1_WA-POSNR

AND KSCHL = 'ZVAT'.

IF SY-SUBRC = 0.

ITAB1_WA-VAT = VATVAL.

SELECT SINGLE KWERT INTO (BASP) FROM KONV WHERE KNUMV = ITAB1_WA-KNUMV AND KPOSN = ITAB1_WA-POSNR

AND KSCHL = 'PR00'.

ITAB1_WA-VALUE = BASP .

ITAB1_WA-TOT = ITAB1_WA-VAT + ITAB1_WA-VALUE.

SELECT SINGLE NAME1 INTO (NAME1) FROM KNA1 WHERE KUNNR = ITAB1_WA-KUNAG.

ITAB1_WA-NAME = NAME1.

SELECT SINGLE J_1ILSTNO INTO (VATNO1) FROM J_1IMOCUST WHERE KUNNR = ITAB1_WA-KUNAG.

ITAB1_WA-VATNO = VATNO1.

MODIFY ITAB1 FROM ITAB1_WA.

CLEAR: VATVAL,BASP,NAME1,VATNO1.

ELSE.

DELETE ITAB1 WHERE BILLNO = ITAB1_WA-BILLNO.

ENDIF.

CLEAR: VATVAL,BASP,NAME1,VATNO1.

ENDLOOP.

Thanks in advance,

Joby

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,969

declare internal tables....

write select statements using <b>FOR ALL ENTRIES</b>

after that loop at first internal table.

use read statements...

append data to final internal table.

endloop

7 REPLIES 7
Read only

Former Member
0 Likes
2,970

declare internal tables....

write select statements using <b>FOR ALL ENTRIES</b>

after that loop at first internal table.

use read statements...

append data to final internal table.

endloop

Read only

anversha_s
Active Contributor
0 Likes
2,969

hi,

1) use

for all entries

2) read the above table inside the loop

Regards

Anver

Read only

Former Member
0 Likes
2,969

Hi John,

Yes the solution is Select with For ALL ENTRIES IN & then read inside a loop.

Here it is how :

SELECT AVBELN AFKART AKNUMV AFKDAT AKUNAG BPOSNR BARKTX BVGBEL INTO TABLE ITAB1

FROM VBRK AS A JOIN VBRP AS B ON AVBELN = BVBELN

WHERE AFKDAT IN DATE AND AFKART NE 'S1' AND BVKBUR IN SOFFICE AND BWERKS IN PLANT.

SELECT KNUMV KPOSN KWERT

INTO TABLE itab5 FROM KONV

FOR ALL ENTRIES IN ITAB1

WHERE KNUMV = ITAB1_WA-KNUMV

AND KPOSN = ITAB1_WA-POSNR

AND KSCHL = 'ZVAT'.

SORT ITAB5 by KNUMV KPOSN.

LOOP AT ITAB1.

Read table itab5 with key

KNUMV = ITAB1_WA-KNUMV

KPOSN = ITAB1_WA-POSNR BINARY SEARCH.

.........

.........

ENDLOOP.

****Similarly replace all ur Select Single Queries inside the loop.

& use Read Table with Binary Search.

This will definately improve the performance of ur Program.

*********Reward useful Answers**************

Read only

Former Member
0 Likes
2,969

John,

Use for all entries here.

SELECT SINGLE KWERT INTO (VATVAL) FROM KONV

INTO TABLE ITAB2

FOR ALL ENTRIES IN ITAB

WHERE KNUMV = ITAB1-KNUMV AND

KPOSN = ITAB1_WA-POSNR

AND KSCHL = 'ZVAT'.

SORT ITAB2 BY KPOSN KSCHL.

LOOP AT ITAB1 INTO ITAB1_WA.

READ TABLE ITAB2 WITH KEY KPOSN = ITAB1_WA-POSNR

AND KSCHL = 'ZVAT' BINARY SEARCH.

ENDLOOP.

Do like above for all select statements which are in loop.

Pls. reward

Read only

Former Member
0 Likes
2,969

Hi Varughesee,

I encountered similar situation in my program & coded it in following way that won't hinder performance.

SELECT AEQUNR BDATAB BILOAN CSWERK

INTO TABLE ITEQ

FROM ( EQUI AS A INNER JOIN EQUZ AS B

ON AEQUNR = BEQUNR ) INNER JOIN ILOA AS C

ON BILOAN = CILOAN

WHERE A~EQART = 'ESTPL'

AND B~IWERK = 'M011'

AND B~INGRP = 'SLM'

AND BDATAB GE SDATE-LOW AND BDATAB LE SDATE-HIGH

AND C~SWERK IN S_WERK.

IF SY-SUBRC = 0.

SELECT ILOAN

TPLNR

MSGRP FROM ILOA

INTO CORRESPONDING FIELDS OF TABLE ITABOA

FOR ALL ENTRIES IN ITEQ

WHERE ILOAN = ITEQ-ILOAN.

SORT ITABOA BY ILOAN.

LOOP AT ITEQ.

READ TABLE ITABOA WITH KEY ILOAN = ITEQ-ILOAN BINARY SEARCH.

IF SY-SUBRC = 0.

MOVE-CORRESPONDING ITABOA TO ITEQ.

MODIFY ITEQ TRANSPORTING TPLNR MSGRP.

ENDIF.

ENDLOOP.

SELECT TPLNR PLTXT FROM IFLO

INTO CORRESPONDING FIELDS OF TABLE ITABLO

FOR ALL ENTRIES IN ITEQ

WHERE TPLNR = ITEQ-TPLNR.

SORT ITABLO BY TPLNR.

LOOP AT ITEQ.

READ TABLE ITABLO WITH KEY TPLNR = ITEQ-TPLNR BINARY

SEARCH.

IF SY-SUBRC = 0.

MOVE-CORRESPONDING ITABLO TO ITEQ.

MODIFY ITEQ TRANSPORTING PLTXT.

ENDIF.

ENDLOOP.

ENDIF.

Hope this helps.

Reward if helpful.

Regards,

Sipra

Read only

Former Member
0 Likes
2,969

Thank you all for your replies.But please note my requirement, I want to select the value (kwert) corresponding to 'PR00' condition if there another record with same key exists with 'ZVAT' condition , otherwise I want to delete the record.Please help me to solve this problem

Read only

0 Likes
2,969

Hi John,

Yes ur requirements r meet.

Here it is how :

SELECT AVBELN AFKART AKNUMV AFKDAT AKUNAG BPOSNR BARKTX BVGBEL INTO TABLE ITAB1

FROM VBRK AS A JOIN VBRP AS B ON AVBELN = BVBELN

WHERE AFKDAT IN DATE AND AFKART NE 'S1' AND BVKBUR IN SOFFICE AND BWERKS IN PLANT.

SELECT KNUMV KPOSN KWERT

INTO TABLE itab5 FROM KONV

FOR ALL ENTRIES IN ITAB1

WHERE KNUMV = ITAB1_WA-KNUMV

AND KPOSN = ITAB1_WA-POSNR

AND KSCHL = 'ZVAT'.

SORT ITAB5 by KNUMV KPOSN.

SELECT KNUMV KPOSN KWERT

INTO TABLE itab6 FROM KONV

FOR ALL ENTRIES IN ITAB1

WHERE KNUMV = ITAB1_WA-KNUMV

AND KPOSN = ITAB1_WA-POSNR

AND KSCHL = 'PR00'.

SORT ITAB6 by KNUMV KPOSN.

LOOP AT ITAB1.

Read table itab5 with key

KNUMV = ITAB1_WA-KNUMV

KPOSN = ITAB1_WA-POSNR BINARY SEARCH.

if sy-subrc = 0.

Read table itab6 with key

KNUMV = ITAB1_WA-KNUMV

KPOSN = ITAB1_WA-POSNR BINARY SEARCH.

if sy-subrc = 0.

............

...............

endif.

endif

ENDLOOP.

GOT IT !!!!!!!!!!!!!!!!

*********Reward useful Answers**************