SQL Update Select combined statement and application

Source: Internet
Author: User
Tags db2 joins

SQL Update Select combined statement and application

October 1, 2013 MK database 19 read 157,639 times

QL Update SELECT statement

The most common update syntax is:

12
table_nameVALUE

If my update value is taken out of a SELECT statement, and there are many columns, this syntax can be cumbersome.

First, to select out on a temporary variable, there are a lot of difficult to save.
Second, assign the variable to the value.

It's a lot of trouble to make a list, can you insert the result of the entire SELECT statement like insert?
Just like the following::

123
INTO table1(C1, C2, C3)(SELECT v1, v2 fromtable2)     

The answer is yes, the specific syntax is as follows:

123456
update table1 aliasset  (Column_name,column_name ) =  (select  (Column_name) from table2where column_name = alias.column_name ) where column_name = value      

Here's an example:
Two tables A, B, to make the Memo field value in B equal to the name value of the corresponding ID in table a
Table A:

1234
ID    1     2     3     photos  

Table B:

1234
ID    ClientName   3

(MS SQL Server) statement:

1
UPDATE b   SET   ClientName    = a. Name from    aWHERE a= b. ID  

(ORALCE) Statement:

1
UPDATE b   SET   (ClientName)    =   (WHERE b= a. ID)  

Update set FROM statement format
When both where and set need to correlate a table for querying, the entire update executes, requiring two scans of the associated table, which is obviously less efficient.

For this scenario, the workaround for Sybase and SQL Server is to use the update ... SET ... From ... WHERE ... Syntax, which is actually getting updated data from the source table.

In SQL, table joins (left joins, right joins, Inner joins, and so on) are often used in SELECT statements.
In fact, in SQL syntax, these connections can also be used for UPDATE and DELETE statements, and the use of joins in these statements often results in a multiplier effect.

12
SET t_orderform=b. L_tuserid onB. l_id=a. ProductID     

Used to synchronize data from two tables!

Syntax supported by both ORALCE and DB2:

1
(A1, A2, A3(SELECT B1, B2WHERE A= B. ID)      

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

1
UPDATE a  = B1= B2 onA= B. ID    

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

1
= B1= B2 fromaWHERE a= B. ID    

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

12
UPDATE ASET(A1, A2, A3)=(select b1, B2 from B where a.id = B .id) in  (select b.id from B where a.id = b.id)     

Original articles such as reproduced please specify, reproduced from:www.aimks.com
This article link: http://www.aimks.com/sql-update-the-select-statement-application.html

SQL Update Select combined statement and application

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.