Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of evaluation of expression in the update-set-clause in oracle database

Tags:

sql

oracle

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

like image 252
Abhijith Madhav Avatar asked Oct 11 '15 10:10

Abhijith Madhav


People also ask

Can we use Order By clause in update statement in Oracle?

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.

What is for update clause in SELECT statement?

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.

What are expressions in Oracle?

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.

What does (+) mean in Oracle?

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.


2 Answers

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.

like image 173
krokodilko Avatar answered Oct 30 '22 13:10

krokodilko


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
like image 45
dnoeth Avatar answered Oct 30 '22 12:10

dnoeth