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: 

updating database table from an internal table

Former Member
0 Kudos

hi! i am tryin to update ztable from an internal table(8 records). but i see that only one record gets updated in ztable. i used the followin code

loop at itab into wa.

modify z3pus from wa.

commit work.

if sy-subrc <>0.

write: / 'fail'.

else.

write: / 'success'.

endif.

endloop.

(used insert and update also not much luck.....)

i got output 'success' 8 times..........but i see only one record in ztable. what could possibly go wrong???????????????

10 REPLIES 10

gopi_narendra
Active Contributor
0 Kudos

Hi Dilip,

when u write in the way u wrote it makes a performance issue as u r trying to update the database table in the loop. so each time it triggers the database and is not suggested.

instead u can update the database Z table just like this.

modify Z3PUS from table itab.

if sy-subrc = 0.

write : / 'Success'.

else.

write : / 'Fail.

endif.

Regards

- Gopi

0 Kudos

You're checking sy-subrc after your COMMIT WORK statement, not the MODIFY statement.

COMMIT WORK always sets sy-subrc to 0, even if MODIFY set a different sy-subrc earlier on.

Hope that helped.

0 Kudos

hey gopi i tried what u said. i got output as success but still i see only one record.

0 Kudos

You can try this .

loop at itab.

Ztab-field1 = itab-field1.

ztab-field2 = itab-field2.

modify ztab.

commit work.

endloop.

Rewards interms of points expected.

Message was edited by:

Manu Kapur

Former Member
0 Kudos

Hi Dilip ,

Did you check if the database table already contains the records havving the primary key same as that of your internal table.

Regards

Arun

anversha_s
Active Contributor
0 Kudos

hi,

before that :- modify statemnt will modify a record if there is record having the same key like that of record in work area.

if there is no record in itab with key of work area record, it will insert a new record in itab.

Rgds

Anver

Former Member
0 Kudos

Hello,

try not to use MODIFY in a loop.

Regards,

Shehryar Dahar

Former Member
0 Kudos

Do not direcly modify from work are.

Atfirst select, if record presents then update.

*select record with

loop at ITAb.

select single * from ztable into wa

where a = ITAB-a and

b = ITAb-b.

*if there is, mark

if sy-dbcnt eq 1.

modify ztable from wa.

commit work.

endif. " modify the record

endloop.

Former Member
0 Kudos

Hi Dilip,

I can see problem due to Primary Key or Index of <b>z3pus</b> table. Please check <b>z3pus</b> table whether you have defined the Primary Key or Index. Write COMMIT WORK after your END...LOOP.

Regards,

Ramki.

Former Member
0 Kudos

Two things

You are getting sy-subrc eq zero because the commit work will set sy-subrc zero.

The problem you are facing related to updation is because either you are trying to insert the same data again and again which violates primary key/unique key.

Try with the statement

insert z3pus from wa.