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

Append table in loop

Former Member
0 Likes
8,698

Hi all,

what i am trying to do is to loop on the table t_po_mat if it found the MATNR move all the given data to table IT_MATNR else append the table.

Here is my code but the thing is when i DEBUG it doesnt go through the ELSE.

LOOP AT IT_MATNR_TEMP.

LOOP AT T_PO_MAT WHERE MATNR = IT_MATNR_TEMP-MATNR.

IF SY-SUBRC = 0.

T_PO_MAT-DELTA = T_PO_MAT-QUANT - T_PO_MAT-DELIV.

MODIFY T_PO_MAT.

MOVE T_PO_MAT-DELTA TO IT_MATNR_TEMP-QUANT.

IT_MATNR_TEMP-INVTY = '01'. "Stock at supplier location

MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.

APPEND IT_MATNR.

ELSE.

MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.

APPEND IT_MATNR.

ENDIF.

ENDLOOP.

ENDLOOP.

Thank you!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,570

LOOP AT IT_MATNR_TEMP.

*--Coment this

*LOOP AT T_PO_MAT WHERE MATNR = IT_MATNR_TEMP-MATNR.

read table t_po_mat key matnr = IT_MATNR_TEMP-MATNR.

IF SY-SUBRC = 0.

T_PO_MAT-DELTA = T_PO_MAT-QUANT - T_PO_MAT-DELIV.

MODIFY T_PO_MAT.

MOVE T_PO_MAT-DELTA TO IT_MATNR_TEMP-QUANT.

IT_MATNR_TEMP-INVTY = '01'. "Stock at supplier location

MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.

APPEND IT_MATNR.

ELSE.

MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.

APPEND IT_MATNR.

ENDIF.

*ENDLOOP.

ENDLOOP.

rgds,

Mano sri

9 REPLIES 9
Read only

manuel_bassani
Contributor
0 Likes
3,570

hI,

after a loop statement, sy-subrc is always 0

Manuel

Read only

0 Likes
3,570

I know that but i put the sy-subrc to be able to have an ELSE...

Read only

Former Member
0 Likes
3,570

LOOP AT IT_MATNR_TEMP.

read table it_po_mat with key MATNR = IT_MATNR_TEMP-MATNR.

IF SY-SUBRC = 0.

T_PO_MAT-DELTA = T_PO_MAT-QUANT - T_PO_MAT-DELIV.

MODIFY T_PO_MAT.

MOVE T_PO_MAT-DELTA TO IT_MATNR_TEMP-QUANT.

IT_MATNR_TEMP-INVTY = '01'. "Stock at supplier location

MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.

modity IT_MATNR.

ELSE.

MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.

APPEND IT_MATNR.

ENDIF.

ENDLOOP.

Read only

Former Member
0 Likes
3,571

LOOP AT IT_MATNR_TEMP.

*--Coment this

*LOOP AT T_PO_MAT WHERE MATNR = IT_MATNR_TEMP-MATNR.

read table t_po_mat key matnr = IT_MATNR_TEMP-MATNR.

IF SY-SUBRC = 0.

T_PO_MAT-DELTA = T_PO_MAT-QUANT - T_PO_MAT-DELIV.

MODIFY T_PO_MAT.

MOVE T_PO_MAT-DELTA TO IT_MATNR_TEMP-QUANT.

IT_MATNR_TEMP-INVTY = '01'. "Stock at supplier location

MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.

APPEND IT_MATNR.

ELSE.

MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.

APPEND IT_MATNR.

ENDIF.

*ENDLOOP.

ENDLOOP.

rgds,

Mano sri

Read only

Former Member
0 Likes
3,570

Melissa,

Modify the Code as below:

LOOP AT IT_MATNR_TEMP.

REad table T_PO_MAT into waT_PO_MAT with key

MATNR = IT_MATNR_TEMP-MATNR.

if sy-subrc = 0.

do the Sucesss case here...

else.

do the failure case here...

endif.

endloop.

Thanks

Kam

Note : allot points for all sucessful psotings

Read only

Former Member
0 Likes
3,570

Here is how you should write it. SY-SUBRC value is always zero inside the loop, because it already found the record based on the condition. You should place it outside the loop.


LOOP AT IT_MATNR_TEMP.
  LOOP AT T_PO_MAT WHERE MATNR = IT_MATNR_TEMP-MATNR.
    T_PO_MAT-DELTA = T_PO_MAT-QUANT - T_PO_MAT-DELIV.
    MODIFY T_PO_MAT.
    MOVE T_PO_MAT-DELTA TO IT_MATNR_TEMP-QUANT.
    IT_MATNR_TEMP-INVTY = '01'. "Stock at supplier location
    MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.
    APPEND IT_MATNR.
  ENDLOOP.
  IF sy-subrc <> 0.
    MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.
    APPEND IT_MATNR.
  ENDIF.
ENDLOOP.

If there is a one to one relationship based on material number, between the two tables, then you can use READ TABLE as suggested by others here.

Srinivas

Read only

Former Member
0 Likes
3,570

I think the other posts are correct, but to improve performance:


sort po_mat by matnr

and then do a binary read when retrieving the record.

Rob

Read only

Former Member
0 Likes
3,570

Hi

First thing , only if the where clause for 2nd loop was successful would it enter the loop - which means the subrc is 0. So always when you are inside second loop your subrc is true. For your code to work , it should be written as :

LOOP AT IT_MATNR_TEMP.

LOOP AT T_PO_MAT

WHERE MATNR = IT_MATNR_TEMP-MATNR.

T_PO_MAT-DELTA = T_PO_MAT-QUANT - T_PO_MAT-DELIV.

MODIFY T_PO_MAT.

MOVE T_PO_MAT-DELTA TO IT_MATNR_TEMP-QUANT.

IT_MATNR_TEMP-INVTY = '01'.

"Stock at supplier location

MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.

APPEND IT_MATNR.

ENDLOOP.

IF sy-subrc <> 0. "Only when 2nd loop fails

MOVE-CORRESPONDING IT_MATNR_TEMP TO IT_MATNR.

APPEND IT_MATNR.

ENDIF.

ENDLOOP.

Above code would generate new entries in IT_MATNR for all matching records with some modified fields , modify IT_PO_MAT for the iterated records.

In case where it does'nt find matching records in IT_PO_MAT , it would generate new record in IT_MATNR.

Regards

Kalidas

Read only

Former Member
0 Likes
3,570

Loop .... where ....

*Inside loop Always sy-subrc = 0

" if where clause success .

endloop .

  • sy-subrc ne 0 "if where clause failed. check this *after the loop .