In which order Oracle SQL update statement changes column values?

前端 未结 2 2038
情话喂你
情话喂你 2021-01-21 07:57

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 conta

相关标签:
2条回答
  • 2021-01-21 08:19
    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

    0 讨论(0)
  • 2021-01-21 08:26

    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;
    
    0 讨论(0)
提交回复
热议问题