4 Leave Cover
4 Leave Cover

Reputation: 1276

In which order Oracle SQL update statement changes column values?

I got this statement

UPDATE TABLE_A SET COL_A = COL_B, COL_B = 0

I am curious about the sequence it execute because I expect COL_A should contain the value of my COL_B and COL_B should be 0 BUT somehow currently both fields are 0.

Upvotes: 3

Views: 1175

Answers (2)

Jeffrey Kemp
Jeffrey Kemp

Reputation: 60272

SQL updates are atomic in nature - there is no concept of "sequence" or "order" in which individual columns are updated. You can put them in any order you like, it doesn't matter.

Conceptually, you can think of it taking the "before" state of the row and changing it into the "after" state of the row. So COL_A will be updated with whatever value was in COL_B prior to the update.

This makes it easy to swap two values:

UPDATE test2 SET A=B, B=A;

Upvotes: 9

Uzipi
Uzipi

Reputation: 26

CREATE TABLE test2(A NUMBER, B NUMBER);

INSERT INTO TEST2 VALUES(1,2);

UPDATE TEST2 SET A=B,B=0;

A=2,B=0 after execute

Upvotes: 1

Related Questions