Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, b.

Asked: August 05, 2016 - 6:20 pm UTC

Last updated: August 07, 2016 - 8:57 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hey Guys,

I have a process where I need to update one column with the value of another if the 1st column is null, when I transfer the value I need to set the value of the second column to null, I am doing this in a single update statement, see example below. Basic stuff right however, I want to understand / ensure that this is valid and not just a side effect and this behavior may change in the future version of Oracle.

titan@SATURN> create table employee
2 (
3 employee_id number,
4 first_name varchar2(30),
5 last_name varchar2(30)
6 );

titan@SATURN> insert into employee values( 1, 'John', 'Doe' );
titan@SATURN> insert into employee values( 1, 'Jack', 'Doe' );
titan@SATURN> insert into employee values( 1, 'Jane', 'Doe' );
titan@SATURN> insert into employee values( 1, null, 'Sam' );
titan@SATURN> insert into employee values( 1, null, 'Pam' );
titan@SATURN> insert into employee values( 1, null, 'Tom' );
--
titan@SATURN> select *
2 from employee;

EMPLOYEE_ID FIRST_NAME LAST_NAME
----------- ------------------------------ ------------------------------
1 John Doe
1 Jack Doe
1 Jane Doe
1 Sam
1 Pam
1 Tom
--
titan@SATURN> update employee
2 set first_name = last_name,
3 last_name = null
4 where first_name is null;
--
titan@SATURN> select *
2 from employee;

EMPLOYEE_ID FIRST_NAME LAST_NAME
----------- ------------------------------ ------------------------------
1 John Doe
1 Jack Doe
1 Jane Doe
1 Sam
1 Pam
1 Tom

Thanks
BC
MT,MI


and Connor said...

No you are fine.

If you think of an update is a "query rows then modify them" operation, then the "query" part is pre-ordained at the initiation of your update statement.

(This is a somewhat of a simplification of what happens in a true multi-user environment - if you search this site for "write consistency", there's plenty of details).

But yes...your approach is valid.

Is this answer out of date? If it is, please let us know via a Comment