How to update data for another table in one UPDATE statement with data from one table

Source: Internet
Author: User
Tags one table

(1) How to update the data of another table in one UPDATE statement with the data of one table
Update AA
Set aa.ac=
(select top 1 dd from CC where CC.AA=AA.AA)
This statement updates each row in the AA table, and the specific execution process is
Iterate through each row of the AA table and then conditionally: Select top 1 dd from CC where CC.AA=AA.AA
Assign the column dd of the corresponding row in the CC table to the column AC of the AA table
(Note: Every column in the cc.aa= (<>) AA.AA,AA table will be updated)
(2) Update AA
Set aa.ac=
(select top 1 dd from CC where CC.AA=AA.AA)
where
Exists (SELECT * from CC where CC.AA=AA.AA)
This statement updates the satisfies condition in the AA table: Exists (SELECT * from CC where CC.AA=AA.AA)
All rows
Update AA
Set aa.ac=
(select top 1 dd from CC where CC.AA=AA.AA)
where
Aa.aa in (select AA from CC where AA.AA=CC.AA)
The statement is equivalent to the above statement (2)
(3) Update (select Aa.ac ac,cc.dd dd from AA,CC where AA.AA=CC.AA)
Set AC=DD
This statement also implements the same functionality, but it seems that SQL Server 2005 does not support this format, but it can be done in Oracle (this statement is simpler)

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.