Application Development 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: 

Update first record out of many availabale.

Former Member
0 Kudos
202

Hello everybody,

There are many records in my table for a given MATNR (non-key field).

If I have to Update only the first record where this MATNR occurs, how shud I proceed ?

Thanks

9 REPLIES 9

Former Member
0 Kudos
131

Hi,

Sort the internal table by matnr and then read table with INDEX 1 and update.

Regds,

Anil

Former Member
0 Kudos
131

sort itab by matnr.

loop at itab.

at new matnr.

update.

endat.

endloop.

Kiran

Former Member
0 Kudos
131

Hi,

Try this.

data: flag.

Sort Itab by matnr.

loop at itab.

on change of matnr.

flag = 'X'.

endon.

if flag = 'X'.

update table itab.

endif.

endloop.

Regards,

Satish

rainer_hbenthal
Active Contributor
0 Kudos
131

What kind of table are you talking? Internal or DB table?

former_member226203
Active Contributor
0 Kudos
131

loop on the internal table and move these entries into a temporary internal table. before updating, check if the entry exists in the temporary internal table. if it doesnot exist update the entry.

loop at itab into wa_itab.

read it_temp into wa_temp with key matnr = wa_itab-matnr.

if sy-subrc = 0.

CONTINUE.

else.

update the entry acc to ur req.

move wa_itab-matnr to it_temp. " it_temp is the temporary table which has matnr.

endif.

endloop.

what happens with this is, for the 1st entry in the loop. it will read the temp table and as it is still empty it updates the first matnr and moves it to the temp table. so if the second entry in the loop is the same matnr again, it will chk in the temp table and it is present there so it goes to CONTINUE statement and gets the next entry in the loop and the process continues.

0 Kudos
131

This is a waste of memory.

the read statement with a where condition will find the first occurence if itab is a standard table. Assigning the result to field symbol made it possible to directly change the value without memory to memory copy.


read table itab where.... assigning <p>.
if sy-subrc = 0.
  <p>-matnr = 1234.
endif.

Read table for a standard tabl will always read from the beginning.

0 Kudos
131

it is for the internal table.

and this is one of the ways that cane be helpful.instead of read we can use FIND to find the entries in the temp table.

0 Kudos
131

Cause you only need to find one value you dont need to use find.

Former Member
0 Kudos
131

data: v_matnr type matnr.

loop at itab.

if v_matnr is initial or v_matnr ne itab-matnr.

v_matnr = itab-matnr.

.......

change wa here

....

modify itab .

endif.

endloop.

Edited by: mayank jain on Aug 13, 2009 3:00 PM