Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a column using a value from the same row in Oracle

Tags:

sql

oracle

I'm creating a SQL command in Oracle to update the value of "LOADDATE" to equal the value of "UPDATEDATE" on my table. What I have here works:

BEGIN
  UPDATE LOAD_SETTINGS 
    SET 
      LOADDATE = (
        SELECT UPDATEDATE 
        FROM LOAD_SETTINGS
        WHERE
          MODEL = 'A001'
          AND OBJECT = 'A'
      )
    WHERE
      MODEL = 'A001'
      AND OBJECT = 'A';
  COMMIT;
END;

The one thing I know for sure about Oracle is that there is a lot that I do not know. Is there a better way to do this using any features of Oracle that I may not be familiar with? Perhaps without having to use a subquery? Or is this the the way to go?

Thanks!

like image 661
Paul Avatar asked Jan 27 '12 21:01

Paul


People also ask

How do you update a column with data from another table in SQL?

In such a case, you can use the following UPDATE statement syntax to update column from one table, based on value of another table. UPDATE first_table, second_table SET first_table. column1 = second_table. column2 WHERE first_table.id = second_table.


1 Answers

Since the where clause on the subquery is the same where on the outer there's no need for the subselect just reference the column directly

UPDATE LOAD_SETTINGS 
SET 
  LOADDATE = UPDATEDATE 

WHERE
  MODEL = 'A001'
  AND OBJECT = 'A';
like image 59
Conrad Frix Avatar answered Nov 01 '22 22:11

Conrad Frix