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

How to update key fields???

former_member321825
Active Participant
0 Likes
12,354

I have created a table ztable with fields field1,field2,field3,field4,field5,field6.

the key fields are field1,field2,field3,field4,field5.

Records in ztable:

FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 FIELD6

1 2 3 4 5 6

1 2 3 5 6 7

now i want to modify 1st row with field4 = 9.

and second row with field 4 = 10.

Please suggest how can i update field4.

since field 4 is a key field new line are inserted .

FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 FIELD6

1 2 3 4 5 6 ....................................old

1 2 3 5 6 7................................old

1 2 3 9 5 6..............................new

1 2 3 9 6 7.............................new

Could anyone help me with a soultion.

Regards,

Rachel

Edited by: Rachel@sap on Dec 30, 2010 6:10 AM

1 ACCEPTED SOLUTION
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
5,986

Hello Rachel,

Your requirement violates the basic principle of RDBMS. All the key fields(of a composite key) form a key which is used to identify a record uniquely.

If you modify a key field, you'll generate a new record with the modified key fields & not overwrite the old one!

So you need to:

1. INSERT / MODIFY the DB table with the modified key field.

2. DELETE the record with the old key field.

Hope i'm clear.

BR,

Suhas

I have created a table ztable with fields field1,field2,field3,field4,field5,field6.

the key fields are field1,field2,field3,field4,field5.

Records in ztable:

FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 FIELD6

1 2 3 4 5 6

1 2 3 5 6 7

now i want to modify 1st row with field4 = 9.

and second row with field 4 = 10.

Please suggest how can i update field4.

since field 4 is a key field new line are inserted .

FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 FIELD6

1 2 3 4 5 6 ....................................old

1 2 3 5 6 7................................old

1 2 3 9 5 6..............................new

1 2 3 9 6 7.............................new

Could anyone help me with a soultion.

Regards,

Rachel

Edited by: Rachel@sap on Dec 30, 2010 6:10 AM

9 REPLIES 9
Read only

Former Member
5,986

Rachel,

You can use the UPDATE statement to update the KEY FIELDS.

UPDATE ztable) 
    SET    field4 = 1 
    WHERE  field1 = ''
     and       field2 = ''. " Tip- in the where clause add all the key fields

Regards,

Madhukar Shetty

Read only

5,986

I know I'm reviving a dead post six years later... But isn't this the right answer for the question asked...??? Please remark if possible.. Helped me in a tight spot.

Read only

Former Member
0 Likes
5,986

hi Rachel

first you need to select the values from z table into an internal table with similar structure.

then loo through internal table with index.

like

loop at itab into wa.

data; l_index type sy-tabix.

l_index = sy-tabix.

field4 = 9.

modify itab from wa index l_index transporting field4.

endloop.

But this will modify all the values of field4 to value 4.

and the rows will be the same.

then insert the values of internal table itab to your Z table. but before that delete all the values from z table because afterall you are going to update it again from internal table.

or check if it is not necessary to delete the values from z table.

thanks

lalit

Read only

Former Member
0 Likes
5,986

Hi,

Please loop the z table move all the key fiel values to another variable/temp structure, (give ur new value too)

loop at ztab

temtab-field1 = 1.

temptab-field2 = 2.

temptab-field3 = 3.

tempfab-field4 = 9.

MODIFY ztab FROM temptab

TRANSPORTING field1 field2 field3 field4 WHERE field1 EQ temptab-field1

AND field2 EQ temptab-field2

AND field3 EQ temptab-field3.

endloop ztab.

Remember you have to give all the key fields in the modify statement.

regards

Mullai

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
5,987

Hello Rachel,

Your requirement violates the basic principle of RDBMS. All the key fields(of a composite key) form a key which is used to identify a record uniquely.

If you modify a key field, you'll generate a new record with the modified key fields & not overwrite the old one!

So you need to:

1. INSERT / MODIFY the DB table with the modified key field.

2. DELETE the record with the old key field.

Hope i'm clear.

BR,

Suhas

Read only

Former Member
0 Likes
5,986

Hi Rachel

You sholud not update the keyfields. Then it will create new record with updated keyfield. It should not be happened , so try to Insert or Modify or Delete the keyfield.

So that you can get the new record with updated keyfield.

Edited by: renu1ece on Dec 30, 2010 6:56 AM

Read only

0 Likes
5,986

Hi Rachel,

As you want to change the key fields of the ZTABLE the only way is to insert a new record(with the new key) and delete the previous one(if not required).

Please go through the link below for Easy user interface(Provided you have authorisation for the same)

Link:[Easiest way to Edit table but be cautious|http://www.kowboyz.nl/blog/edit-sap-tables/]

Hope it helps you,

Regards,

Abhijit G. Borkar

Read only

0 Likes
5,986

Thanks for the solution.

Regards,

Rachel

Read only

0 Likes
5,986

Hey Rachel,

I think you forgot something.

Regards,

Abhijit G. Borkar