Oracle Updates another table field with one table field three ways ____oracle

Source: Internet
Author: User
Tags one table

1. Update (SELECT ..... ) Set column1 = Column2;

Update (select Iy.company_name company_name1, CC.COMPANY_NAME_JC company_name2
From Income_year_item iy, City_company cc
where Iy.company_code = Cc.code
)
Set company_name1 = Company_name2;

2. Only single-line subquery s

Update City_project_scale_info C Set
(c.value) = (
Select D.value from City_project_scale_info @test d where d.project_id = ' 7d7fd580a06240b2a9137dc2bbe831e9 '
and d.project_id = c.project_id and C.company_code = D.company_code
)
where exists (
Select 1 from City_project_scale_info @test d where d.project_id = ' 7d7fd580a06240b2a9137dc2bbe831e9 '
and d.project_id = c.project_id and C.company_code = D.company_code
)

If you pay particular attention to the WHERE clause here, you can try to do the following sentence without writing a WHERE clause, and you will make many of the values in the City_project_scale_info empty.

This is because if the WHERE clause is not written in an Oracle UPDATE statement, Oracle will update all of the values by default, even if you use subqueries here and some values are not found in subqueries, you assume that Oracle might skip these values, You are wrong, Oracle will update the value of the row to null.

3. Use Merg inot statement

--Update Production base fields
Merge into City_cfg_data_column_common CF1 using
City_cfg_data_column_common2 CF2 on
(Cf1.resourceid = Cf2.resourceid)
When matched then
Update Set cf1.template_type = Cf2.template_type,
Cf1.chinese_name = Cf2.chinese_name,
Cf1.column_name = Cf2.column_name,
Cf1.column_type = Cf2.column_type,
Cf1.column_size = Cf2.column_size
When not matched then
Insert (cf1.resourceid,cf1.template_type,cf1.chinese_name,cf1.column_name,cf1.column_type,cf1.column_size
, Cf1.is_can_edit,cf1.is_unique,cf1.is_can_cover,cf1.show_order)
VALUES (cf2.resourceid,cf2.template_type,cf2.chinese_name,cf2.column_name,cf2.column_type,cf2.column_size
, Cf2.is_can_edit,cf2.is_unique,cf2.is_can_cover,cf2.show_order)

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.