In a table I have column A and B. I want to update A using the value of B and then update B to a new value. This has to be done atomically.
I am trying something like this
-- Intially A = 1, B = 2
UPDATE T SET A = B, B = 10 WHERE ID = 1;
-- Now A = 2, B = 10
Though this is working, I am unable to find documentation which guarantees me that A = B is evaluated first and B = 10 is evaluated later.
I looked through the oracle sql reference of the update statement
If an UPDATE statement includes an ORDER BY clause, the rows are updated in the order specified by the clause. This can be useful in certain situations that might otherwise result in an error.
The FOR UPDATE clause is an optional part of a SELECT statement. Cursors are read-only by default. The FOR UPDATE clause specifies that the cursor should be updatable, and enforces a check during compilation that the SELECT statement meets the requirements for an updatable cursor.
An expression is a combination of one or more values, operators, and SQL functions that evaluate to a value. An expression generally assumes the data type of its components. Expressions have several forms. The sections that follow show the syntax for each form of expression.
The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key. If there's no matching row, return null.
You can find this in SQL standard, which defines general rules.
Oracle certainly conforms to this standard.
See here - SQL 92:
http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
Page 393, chapter "13.9 <update statement: positioned>", point 6)
6) The <value expression>s are effectively evaluated before updat- ing the object row. If a contains a reference to a column of T, then the reference is to the value of that column in the object row before any value of the object row is updated.
Consider a general update syntax:<
UPDATE ....
SET <object column 1> = <value expression 1>,
<object column 2> = <value expression 2>,
......
<object column N> = <value expression N>;
The rule #6 says that all expressions on right side are evaluated first, before updating of any column in the row.
Only old row's values (before the update) are considered while evaluating all expressions.
In a RDBMS (unlike a programming language) there's no order of evaluation, it's all done at once. It's like you set variables to the previous value first and then use those variables:
SET a=b, b=a
simply switches a
and b
.
Warning: Only MySQL does it totally wrong, resulting in both set to the same b
value, here you'll need a temp variable like:
SET temp=b, b=a, a = temp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With