About the UPDATE statement in SQL

Source: Internet
Author: User
Tags one table

Today, in SQL database operation, you need to update the data in one table to another table, but I don't know how to succeed in the way I used to. The code is as follows:

Update table1 a       set a.col1=b.col2    from  table2 b  where a.c= B.C

The above notation appears to be achievable in the context of Oracle, which is not validated in SQL Server anyway. The internet looked for a bit and finally found a solution, as follows:

Syntax supported by both ORALCE and DB2:

UPDATE A SET (A1, A2, A3) = (SELECT B1, B2, B3 from B WHERE a.id = b.id)

MS SQL Server does not support such a syntax, the corresponding wording is:

UPDATE A SET A1 = B1, A2 = B2, A3 = B3 from A left JOIN B on a.id = b.ID

Personal feeling MS SQL Server's update syntax is more powerful. MS SQL Server notation:

UPDATE A SET A1 = B1, A2 = B2, A3 = B3 from A, B WHERE a.id = b.id

The syntax for Oracle and DB2 is more cumbersome, as follows:

UPDATE A SET (A1, A2, A3) = (select B1, B2, B3 from b where a.id = b.id) where ID in (select b.ID from B where a.id = b.id)

About the UPDATE statement in SQL

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.