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: 

add record

Former Member
0 Kudos

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

former_member156446
Active Contributor
0 Kudos

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

Former Member
0 Kudos

hey,

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

thanks

Ajantha R

former_member156446
Active Contributor
0 Kudos

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

0 Kudos

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

0 Kudos

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

cheers,

Sushil Joshi

0 Kudos

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

Former Member
0 Kudos

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