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

problem using READ statement in loop.

Former Member
0 Likes
731

Hi,

i have few read statements within loop ...endloop as shown:

LOOP AT t_pbim INTO wa_pbim.

READ TABLE t_zpp_material

INTO wa_zpp_material

WITH KEY smatn_new = wa_pbim-matnr.

wa_final-product = wa_zpp_material-product.

wa_final-smatn_old = wa_zpp_material-smatn_old .

wa_final-smatn_new = wa_zpp_material-smatn_new .

wa_final-plant = wa_zpp_material-plant.

wa_final-zdate1 = wa_zpp_material-zdate1.

READ TABLE t_pbed

INTO wa_pbed

WITH KEY bdzei = wa_pbim-bdzei .

wa_final-perxx = wa_pbed-perxx.

READ TABLE t_mdbs

INTO wa_mdbs

WITH KEY matnr = wa_pbim-matnr.

wa_final-val = wa_mdbs-val.

wa_final-meins = wa_mdbs-meins.

append wa_final to t_final.

endloop.

The second read statement finds one matching records with the key value given.but as it is in the loop,that single entry is duplicated in t_final.

please mention how to resolve the issue.

7 REPLIES 7
Read only

Former Member
0 Likes
704

Hi,

THe code is absolutely fine. But if you want unique data based on BDZEI, then delete duplicates from T_PBIM

<b>SORT T_PBIM by BDZEI.

DELETE ADJACENT DUPLICATES FROM T_PBIM COMPARING BDZEI.</b>

LOOP AT t_pbim INTO wa_pbim.

READ TABLE t_zpp_material

INTO wa_zpp_material

WITH KEY smatn_new = wa_pbim-matnr.

wa_final-product = wa_zpp_material-product.

wa_final-smatn_old = wa_zpp_material-smatn_old .

wa_final-smatn_new = wa_zpp_material-smatn_new .

wa_final-plant = wa_zpp_material-plant.

wa_final-zdate1 = wa_zpp_material-zdate1.

READ TABLE t_pbed

INTO wa_pbed

WITH KEY bdzei = wa_pbim-bdzei .

wa_final-perxx = wa_pbed-perxx.

READ TABLE t_mdbs

INTO wa_mdbs

WITH KEY matnr = wa_pbim-matnr.

wa_final-val = wa_mdbs-val.

wa_final-meins = wa_mdbs-meins.

append wa_final to t_final.

endloop.

Read only

former_member189629
Active Contributor
0 Likes
704

Hi Deb,

clear the table headers before each READ.

<b><REMOVED BY MODERATOR></b>

Karthik

Message was edited by:

Alvaro Tejada Galindo

Read only

0 Likes
704

which table headers to be cleared?

Read only

0 Likes
704

Hi,

if you want to ensure the result is unique, do not use append but use COLLECT wa INTO itab.

Regards,

Gianpietro

Read only

Former Member
0 Likes
704

LOOP AT t_pbim INTO wa_pbim.

READ TABLE t_zpp_material

INTO wa_zpp_material

WITH KEY smatn_new = wa_pbim-matnr.

wa_final-product = wa_zpp_material-product.

wa_final-smatn_old = wa_zpp_material-smatn_old .

wa_final-smatn_new = wa_zpp_material-smatn_new .

wa_final-plant = wa_zpp_material-plant.

wa_final-zdate1 = wa_zpp_material-zdate1.

READ TABLE t_pbed

INTO wa_pbed

WITH KEY bdzei = wa_pbim-bdzei .

wa_final-perxx = wa_pbed-perxx.

READ TABLE t_mdbs

INTO wa_mdbs

WITH KEY matnr = wa_pbim-matnr.

wa_final-val = wa_mdbs-val.

wa_final-meins = wa_mdbs-meins.

append wa_final to t_final.

endloop.

add the following code to ur code...

delete the entry from second internal table after first occurance of the loop..

the syntax will be like this,,,

delete it_2 from wa_2 index sy-tabix.

ok

<b><REMOVED BY MODERATOR></b>

Message was edited by:

Alvaro Tejada Galindo

Read only

Former Member
0 Likes
704

try the read statement with Binary search

or use DELETE ADJACENT DUPLICATES

Read only

Former Member
0 Likes
704

Hi,

Whenever you use a read statement always check for sy-subrc .

modify the code as follows:

LOOP AT t_pbim INTO wa_pbim.

READ TABLE t_zpp_material

INTO wa_zpp_material

WITH KEY smatn_new = wa_pbim-matnr.

wa_final-product = wa_zpp_material-product.

wa_final-smatn_old = wa_zpp_material-smatn_old .

wa_final-smatn_new = wa_zpp_material-smatn_new .

wa_final-plant = wa_zpp_material-plant.

wa_final-zdate1 = wa_zpp_material-zdate1.

READ TABLE t_pbed

INTO wa_pbed

WITH KEY bdzei = wa_pbim-bdzei .

<b>if sy-subrc eq 0.</b>

wa_final-perxx = wa_pbed-perxx.

<b>endif.</b>

READ TABLE t_mdbs

INTO wa_mdbs

WITH KEY matnr = wa_pbim-matnr.

<b>if sy-subrc eq 0.</b>

wa_final-val = wa_mdbs-val.

wa_final-meins = wa_mdbs-meins.

<b>endif.</b>

append wa_final to t_final.

endloop.

regards,

Navneeth K.