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: 

Regarding "Update .. From Table" Statement

former_member202684
Contributor
0 Kudos
230

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.

1 ACCEPTED SOLUTION

former_member222709
Contributor
0 Kudos
198

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.

4 REPLIES 4

Former Member
0 Kudos
198

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

kesavadas_thekkillath
Active Contributor
0 Kudos
198

Please read SAP documentation on UPDATE/ MODIFY/INSERT. You will understand.

former_member222709
Contributor
0 Kudos
199

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.

0 Kudos
198

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