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

Former Member
0 Likes
699

Hi all

In the below code you can see am using two LOOP AT statements.So its taking a long time to execute how can i use only one loop condition and get my desired output.Please help.

Vijay

SELECT * FROM s023 INTO

CORRESPONDING FIELDS OF TABLE IT_s023 WHERE

WERKS = P_WERKS AND

SPTAG IN P_SPTAG .

SELECT * FROM S026 INTO CORRESPONDING FIELDS OF TABLE

IT_S026 WHERE WERKS = P_WERKS AND

SPTAG IN p_sptag .

LOOP AT IT_S026.

IT_REPORT-ENMNG = IT_S026-ENMNG .

IT_REPORT-SPTAG = IT_S026-SPTAG .

IT_REPORT-MATNR = IT_S026-MATNR .

ENDLOOP.

LOOP AT IT_S023.

ADD IT_s023-WEMNG TO Z.

IT_REPORT-WEMNG = IT_S023-WEMNG.

APPEND IT_REPORT.

ENDLOOP.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
665

Hi,

You can do as below :

loop at it_s023.

read table it_s026 wiht key ssour = it_s023-ssour

all key field.

if sy-subrc eq 0.

append the values of it_s023 and it_s026 to it_report.

clear it_report.

endif.

endloop.

Thanks,

Sriram Ponna.

7 REPLIES 7
Read only

Former Member
0 Likes
665

SELECT ENMNG SPTAG MATNR FROM s023 INTO

(IT_S026-ENMNG IT_S026-SPTAG IT_S026-MATNR ) WHERE

WERKS = P_WERKS AND

SPTAG IN P_SPTAG .

SELECT WEMNG FROM S026 INTO IT_S026-WEMNG WHERE

WERKS = P_WERKS AND

SPTAG IN p_sptag .

now u can write

LOOP AT IT_S026.

IT_REPORT-ENMNG = IT_S026-ENMNG .

IT_REPORT-SPTAG = IT_S026-SPTAG .

IT_REPORT-MATNR = IT_S026-MATNR .

ENDLOOP.

LOOP AT IT_S023.

ADD IT_s023-WEMNG TO Z.

IT_REPORT-WEMNG = IT_S023-WEMNG.

APPEND IT_REPORT.

ENDLOOP.

Edited by: vibhuti ghadoliya on Jan 4, 2008 4:43 PM

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
665

Have u bugged your code frm the beginning.,...i guess its slowing down in u r query.

Just remove * from the select and provide appropriate field names, avoid corresponding

LOOP AT IT_S026.

IT_REPORT-ENMNG = IT_S026-ENMNG .

IT_REPORT-SPTAG = IT_S026-SPTAG .

IT_REPORT-MATNR = IT_S026-MATNR .

ENDLOOP.

LOOP AT IT_S023.

ADD IT_s023-WEMNG TO Z.

IT_REPORT-WEMNG = IT_S023-WEMNG.

APPEND IT_REPORT.

ENDLOOP.

in the first loop there is no append statement.

the output when printed will be value in IT_REPORT-WEMNG.

Specify your requirement...the code above is incorrect

Read only

Former Member
0 Likes
666

Hi,

You can do as below :

loop at it_s023.

read table it_s026 wiht key ssour = it_s023-ssour

all key field.

if sy-subrc eq 0.

append the values of it_s023 and it_s026 to it_report.

clear it_report.

endif.

endloop.

Thanks,

Sriram Ponna.

Read only

abdulazeez12
Active Contributor
0 Likes
665

Hi

Include all the key fields from both the tables in your selection.

Select SSOUR VRSIO SPMON SPTAG SPWOC SPBUP WERKS DISPO MATNR WEMNG from S023 into table IT_S023 where <conditions>.

Select ENMNG SPTAG MATNR from S026 into table IT_S026 for all entries in IT_S023 where

SSOUR = IT_S023-SSOUR and

VRSIO = IT_S023-VRSIO and

SPMON = IT_S023-SPMON and

SPTAG = IT_S023-SPTAG and

SPWOC.....

SPBUP.....

WERKS....

till MATNR = IT_S023-MATNR.

then, loop the table IT_S026 and read from IT023 while populating IT_REPORT as below:

Loop at IT_S026.

IT_REPORT-ENMNG = IT_S026-ENMNG .

IT_REPORT-SPTAG = IT_S026-SPTAG .

IT_REPORT-MATNR = IT_S026-MATNR

read table IT_S023 with key <all key fields>

if sy-subrc eq 0.

IT_REPORT-WEMNG = IT_S023-WEMNG.

endif.

append it_result.

Endloop.

This code will resolve ur issues.

Cheers

Shakir

Read only

0 Likes
665

If you've got some common fields why not use a JOIN .

You should only need ONE select statement and no loop as your data selection will be handled by the ON conditions in the join.

Cheers

jimbo

Read only

Former Member
0 Likes
665

data: wa1 like line of it_s026,

wa2 like line of it_s023.

SELECT * FROM S026 INTO CORRESPONDING FIELDS OF

wa1 WHERE WERKS = P_WERKS AND

SPTAG IN p_sptag .

IT_REPORT-ENMNG = wa1-ENMNG .

IT_REPORT-SPTAG = wa1-SPTAG .

IT_REPORT-MATNR = wa1-MATNR .

SELECT * FROM s023 INTO

CORRESPONDING FIELDS OF wa2 WHERE

WERKS = P_WERKS AND

SPTAG IN P_SPTAG .

ADD wa2-WEMNG TO Z.

IT_REPORT-WEMNG = wa2-WEMNG.

APPEND IT_REPORT.

endselect.

endselect.

Read only

0 Likes
665

Hi RAMU

for efficient ABAP coding you should always avoid wherever possible the SELECT .... ENDSELECT sequence.

Try and do ALL the DB access in ONE SQL statement.

SELECT .... ENDSELECT is a Very Expensive Construct when you are trying for efficient code.

Nesting them is EVEN MORE expensive -- Do Not Use !!!!

If I were an ABAP manager I would BAN the use of the select....endselect statement.

It IS possible to avoid the use of this statement 99.99% of the time.

Cheers

jimbo