In the update operation for a table, in many cases, you need to refer to data other than the table you want to update in an expression. Like SQL Server provides the FROM clause of the update, you can connect the table you want to update with its data source. Although only one table can be updated, it is possible to reference data other than the table to be updated in an update expression by connecting the table that will be updated with its data source.
For example:
UPDATE Table2
SET Table2.colb = Table2.colb + Table1.colb
From Table2
INNER JOIN Table1
On (Table2.cola = Table1.cola);
The actual update is performed on the table to be updated, not on the new result set formed by the FROM clause.
Oracle does not have the update from syntax, and can be used in two ways to achieve the same functionality:
1: Subquery update A SET a.name= (SELECT b.name from B WHERE b.id=a.id), this query depends on the specific circumstances to see if the work is as follows
(1) Single row
UPDATE A
SET a.name= (SELECT b.name from B WHERE b.id=a.id)
WHERE a.id in (SELECT ID from B);
(2) Multi-column
UPDATE Order_rollup
SET (Qty,price) = (SELECT sum (qty), SUM (price) from Order_lines WHERE customer_id= ' KOHL ')
WHERE cust_id= ' KOHL ' and order_period=to_date (' 01-oct-2000 ')
Source
Update "shop_goods_date_copy" copy set (Hotel_info_name,
HOTEL_BODY_ID,
up_down_status,hotel_id) = (
SELECT
Hotel_info_name,
Sghi. HOTEL_BODY_ID,
Up_down_status,
SGHB. hotel_id
From Shop_goods_hotel_info Sghi
Left JOIN (
SELECT
HOTEL_BODY_ID,
hotel_id
From Shop_goods_hotel_b
) SGHB on Sghi. HOTEL_BODY_ID=SGHB. hotel_body_id
WHERE Sghi. Hotel_info_id=copy. hotel_info_id
)
2: Using views to do
UPDATE (SELECT a.name aname,b.name bname from A, WHERE a.id=b.id)
SET Aname=bname;
For example:
UPDATE TableA A
SET a.fieldforupdate = (SELECT b.fieldsource from TableB b WHERE A.keyfield = B.keyfield)
where EXISTS (SELECT b.fieldsource from TableB b WHERE A.keyfield = B.keyfield)
There are three points to note:
- For a given A.keyfield value, SELECT b.fieldsource from TableB b WHERE A.keyfield = B.keyfield value can only be a unique value and cannot be a multivalued value.
- In most cases, the WHERE EXISTS clause of the last face is important, otherwise you will get the wrong result.
- Limitations for View updates:
If the view is based on connections from multiple tables, the ability to record the user update view is limited. The base table of the view cannot be updated unless update involves only one table and the View column contains the entire primary key of the table being updated.
UPDATE from Solution in Oracle