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:
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:
Table B:
(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