Source: http://www.aimks.com/sql-update-the-select-statement-application.html
QL Update SELECT statement
The most common update syntax is:
UPDATE table_name SET = = VALUE
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::
INSERT into table1 (c1, C2, C3) (SELECT from Table2)
The answer is yes, the specific syntax is as follows:
UPDATE table1 alias SET = (SELECT (column_name, column_name) from table2WHERE= alias.column_name)WHERE= 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:
ID 1 23 Sheets
Table B:
(MS SQL Server) statement:
UPDATE b SET ClientName = a.name from a b WHERE = b.ID
(ORALCE) Statement:
UPDATE b SET (ClientName) = (SELECTfromWHERE = 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.
UPDATE SET = B.l_tuserid from Left JOIN T_productinfo on b.l_id=A.productid
Used to synchronize data from two tables!
Syntax supported by both ORALCE and DB2:
UPDATE SET = (SELECTfromWHERE= b.id)
MS SQL Server does not support such a syntax, the corresponding wording is:
UPDATE A SET= = = B3 from the left JOIN on = b.ID
Personal feeling MS SQL Server's update syntax is more powerful. MS SQL Server notation:
UPDATE SET = = = from WHERE = b.ID
The syntax for Oracle and DB2 is more cumbersome, as follows:
UPDATE SET = (SELECTfromWHERE= b.id) WHERE inch (SELECTfromWHERE= b.id)
SQL Update Select combined statement and application