Oracle update from Solution

Source: Internet
Author: User

In table update operations, in many cases, data other than the table to be updated must be referenced in the expression. Like SQL Server provides the update from clause, it can connect the table to be updated with other data sources. Although only one table can be updated, by connecting the table to be updated with other data sources, you can reference data other than the table to be updated in the update expression.
For example:

Update Table2
Set table2.colb = table2.colb + table1.colb
From Table2
Inner join Table1
On (table2.cola = table1.cola );

The actual update operation is performed on the table to be updated, rather than on the new result set formed by the from clause.

Oracle does not have the update from syntax. You can use the following two methods to implement the same function:
1: subquery update a set a. Name = (select B. name from B where B. ID = A. ID)
(1) Single Column
Update

Set a. Name = (select B. name from B where B. ID = A. ID)

Where a. ID in (select ID from B );

(2) multiple columns
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 ')

2: Using views
Update (select a. Name aname, B. Name bname from a, B where a. ID = B. ID)
Set aname = bname;

For example:

Update tablea
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)

Note the following three points:
1. For a given a. keyfield value, the value of select B. fieldsource from tableb B where a. keyfield = B. keyfield can only be a unique value, not a multi-value.
2. In most cases, the where exists clause at the end is important. Otherwise, an error is returned.
3. Restrictions on view update:
If a view is connected to multiple tables, the user's ability to update view records is limited. The base table of the view cannot be updated unless update only involves one table and the View column contains the entire primary key of the updated table.

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.