简体   繁体   中英

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.

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;
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM