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

add record

Former Member
0 Likes
765

hi,

i need to add record to sap standard table.

if i use insert then repeated key field not allowed me to do so.

should i do like this.

if record found use update whereas if record not found use insert.

loop

UPDATE ... SET ...

WHERE ....

IF sy-subrc = 0.

COMMIT WORK.

ELSE.

insert ....

COMMIT WORK

ENDIF.

endloop.

thanks

1 ACCEPTED SOLUTION
Read only

former_member156446
Active Contributor
0 Likes
736

if the strucuture of the internal table which you are looping is same as the database table, with out any loop you can directly put..

modify <DBtable> from <itab>."DBtable and Itab need to be of same structure

6 REPLIES 6
Read only

Former Member
0 Likes
736

hey,

use modify stmt.... it will modify the record if it existing otherwise will add a new record ........

thanks

Ajantha R

Read only

former_member156446
Active Contributor
0 Likes
737

if the strucuture of the internal table which you are looping is same as the database table, with out any loop you can directly put..

modify <DBtable> from <itab>."DBtable and Itab need to be of same structure

Read only

0 Likes
736

hi,

if i use modify from table like this

modify <DBtable> from <itab>."DBtable and Itab need to be of same structure

do i still need to add in commit work?

or something like this?

if sy-subrc = 0

commit work.

else

rollback work.

endif.

thanks

Read only

0 Likes
736

Yes you would still need to add the commit work statement.

cheers,

Sushil Joshi

Read only

0 Likes
736

If b4 the program ends if u need to read the value u updated then its necessary to write commit work ortherwise once the program ends all the cahnges are committed to the database

but its preferable to write commit

Read only

Former Member
0 Likes
736

Hi,

Its always better to commit work to be on safer side.

Its always better to use modify if we need to add a record or update a reacord.

MODIFY dbtable FROM TABLE itab1. 
Or
MODIFY dbtable FROM wa_itab1

The MODIFY statement inserts one or several lines specified in source in the database table specified in target, or overwrites existing lines.

So we can have like this :

loop at itab1 into wa_itab1
MODIFY dbtable FROM wa_itab1
IF sy-subrc = 0.
COMMIT WORK.
ELSE.
ROLLBACK WORK.
ENDIF.
endloop.

Or.

MODIFY dbtable FROM TABLE itab1.
IF sy-subrc = 0.
COMMIT WORK.
ELSE.
ROLLBACK WORK.
ENDIF.

Hope this helps you.

plz reward if useful.

thanks,

dhanashri.

Edited by: Dhanashri Pawar on Jul 15, 2008 6:34 AM