2011 Dec 21 11:18 AM
Hi,
I have an existing piece of code which I'm trying to understand.
There are two scenarios regarding the UPDATE statement.
1) Scenario 1:
There exists a database table with around 100 or more fields/columns [Eg: DB_TABLE1].
An Internal Table exists with the same key as the above database table [EG: I_TABLE1].
code:
loop at I_TABLE1.
update DB_TABLE1
set kf1 = I_TABLE1-kfa
kf2 = I_TABLE1-kfb
kf3 = I_TABLE1-kfc
kf4 = I_TABLE1-kfd
where key1 = I_TABLE1-keya AND key2 = I_TABLE1-keyb.
Endloop.
In the above scenario the records in database table [DB_TABLE1] with matching condition are updated and the rest of the fields are not touched. - Correct me if I am wrong.
2) Scenario 2:
There exists a database table with around 100 or more fields/columns [Eg: DB_TABLE1].
Two Internal Tables exist with the same key as the above database table [EG: I_TABLE1 And I_TABLE2].
Code part a:
SELECT * FROM DB_TABLE1 INTO TABLE I_TABLE2
WHERE KF1 <> 0.
Internal Table 1 has some data.
Code part b:
loop at I_TABLE1.
update DB_TABLE1
set kf1 = I_TABLE1-kfa
kf2 = I_TABLE1-kfb
kf3 = I_TABLE1-kfc
kf4 = I_TABLE1-kfd
where key1 = I_TABLE1-keya AND key2 = I_TABLE1-keyb.
Endloop.
Code part c:
update DB_TABLE1 FROM TABLE I_TABLE2.
In the second scenario what will be the final effect of DB_TABLE1?
As i understand it, there is no use of Code part b. Am I right?
I thought that the records in I_TABLE2 with matching key in DB_TABLE1 will be updated and the rest of the records will be deleted.
But actually there is not change in the DB_TABLE1 when the above code is executed.
Help is appreciated.
Thanks.
2011 Dec 21 11:33 AM
Hi Joey,
Scenario 1: In the above scenario the records in database table DB_TABLE1 with matching condition are updated and the rest of the fields are not touched.
- That's right.
Scenario 2: As i understand it, there is no use of Code part b.
- Exactly because of the following reasons:
1. Code part a shows data selected from DB_TABLE1 INTO TABLE I_TABLE2.
2. Code part c shows DB_TABLE1 updated from TABLE I_TABLE2 whereas Code part b uses ONLY internal table I_TABLE1.
3. If there are any operations performed in Code Part b on 'I_TABLE2', then this would have been different.
Regards,
Pranav.
2011 Dec 21 11:25 AM
UPDATE.. FROM TABLE only updates records matching with the key, it does not delete those that don't match in the db table. Read the documentation of UPDATE statement by doing f1 in ABAP editor
2011 Dec 21 11:27 AM
Please read SAP documentation on UPDATE/ MODIFY/INSERT. You will understand.
2011 Dec 21 11:33 AM
Hi Joey,
Scenario 1: In the above scenario the records in database table DB_TABLE1 with matching condition are updated and the rest of the fields are not touched.
- That's right.
Scenario 2: As i understand it, there is no use of Code part b.
- Exactly because of the following reasons:
1. Code part a shows data selected from DB_TABLE1 INTO TABLE I_TABLE2.
2. Code part c shows DB_TABLE1 updated from TABLE I_TABLE2 whereas Code part b uses ONLY internal table I_TABLE1.
3. If there are any operations performed in Code Part b on 'I_TABLE2', then this would have been different.
Regards,
Pranav.
2011 Dec 21 11:44 AM
Thanks Pranav!
I just needed confirmation on this.
@rest: I have read the SAP documentation and couldn't exactly understand what happens in these scenarios and hence posted the question.
Thanks,
Joey