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: 

How to update database table with key field?

Former Member
0 Kudos
4,836

Hello Experts,

I have a database table with following fields

MATNR - Key

SSOUR - Key

KUNNR - Key

MENG1

MENG2

MENG3

And this table contains records like...

MSD50001 R 1000001 5.30 2.30 5.25

MSD50002 R 1000002 5.30 2.30 5.25

MSD50003 R 1000003 5.30 2.30 5.25

MSD50005 R 1000004 5.30 2.30 5.25

MSD50004 R 1000005 5.30 2.30 5.25

I have an internal table with same fields of above database table.

MSD50001 A 1000001 5.30 2.30 5.25

MSD50002 A 1000002 5.30 2.30 5.25

MSD50003 A 1000003 5.30 2.30 5.25

MSD50005 A 1000004 5.30 2.30 5.25

MSD50004 A 1000005 5.30 2.30 5.25

MSD50006 A 1000006 5.30 2.30 5.25

I want to update the DB table with following internal table records.

If internal table records = db table records are same then Update....else insert from internal table to db table.

But here, SSOUR is key field so i am not able to use

MODIFY dbtab from itab.

It results me , entries in internal table are inserted into db table.

So i have double records.

Is there any statement which updates the key field? and if no fields in db table then insert it?

Regards

RH

5 REPLIES 5

Former Member
0 Kudos
494

Hello,

If you want to update only key fields

you use modify statement wht all the fields you want to update

Modify db from wa_area index sy-tabix transporting key fileds1 field2 field3.

Former Member
0 Kudos
494

hi,

u cannot update akey field.u can update only non key fields by using key field as a selection criteria.For example if u go to sm30 if u enter any table for inserting values, the key field will always be greyed out it is not succumbed to chnges.but u can insert value into ,if ur inserting and in the insert staemnet if u have key field which is already present in the table it will return sy-subrc = 4 otherwise it will insert the record and return 0.

eg for update :

UPDATE zdm_wtyprof SET upload_status = 'S'

WHERE spart = wa_upd-spart.

here spart is the key field and it will update the status field in the table as S.

if updated succesfully it will return 0.

SIMILARLY U CAN USE select.

Former Member
0 Kudos
494

Hi

You can not change the Primary key values. If the table is custom, then delete the current record and re insert the same using MODIFY/INSERT/UPDATE.

Regards

Shiva

Former Member
0 Kudos
494

Hi,

U can't update the key field.

U can do onething for ur requirement,

Delete entries in Database table where combination of MATNR and KUNNR and then u insert the new combination of MATNR SSOUR KUNNR.

Former Member
0 Kudos
494

Thanks