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

updating database table from an internal table

Former Member
0 Likes
3,372

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???????????????

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
Read only

gopi_narendra
Active Contributor
0 Likes
1,736

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

Read only

0 Likes
1,736

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.

Read only

0 Likes
1,736

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

Read only

0 Likes
1,736

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

Read only

Former Member
0 Likes
1,736

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

Read only

anversha_s
Active Contributor
0 Likes
1,736

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

Read only

Former Member
0 Likes
1,736

Hello,

try not to use MODIFY in a loop.

Regards,

Shehryar Dahar

Read only

Former Member
0 Likes
1,736

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.

Read only

Former Member
0 Likes
1,736

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.

Read only

Former Member
0 Likes
1,736

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.