UPDATE from Solution in Oracle

Source: Internet
Author: User
Tags one table

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:

      1. 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.
      2. In most cases, the WHERE EXISTS clause of the last face is important, otherwise you will get the wrong result.
      3. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.