Here is an example:
two tables a, b, b, want to make the memo
field value is equal to a table corresponding to id
the name
values of
table a:
id, name
1 王
2 李
3 张
Table b:
id,ClientName
1
2
3
(MS SQL Server) statement:update b set ClientName = a.name from a,b where a.id = b.id
(Oralce) statement:update b set (ClientName) = (SELECT name FROM a WHERE b.id = a.id)
update set from
Statement format
When both where
and set
need to be associated with a table for query, the entire update
execution requires two scans of the associated table, which is obviously inefficient.
In this case, Sybase
and SQL SERVER
the solution is to use UPDATE...SET...FROM...WHERE...
grammar, in fact, to get an update from the source table.
In SQL, table joins ( left join、right join、inner join
and other) are often used select
statement, in fact, in SQL syntax, these connections can also be used update
and delete
statements, in these statements join
often get a multiplier effect.
Update table1 set table1.xxx=b.yyy from table1 a left join table2 b on b.ttt=a.ttt
Used to synchronize the data of the two tables!
Oralce
DB2
Syntax supported by both and :
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 grammar, the corresponding writing is:
UPDATE A SET A1 = B1, A2 = B2, A3 = B3 FROM A LEFT JOIN B ON A.ID = B.ID
Personally feel that MS SQL Server
the Update
grammatical function is more powerful.
The wording of MS SQL SERVER:
UPDATE A SET A1 = B1, A2 = B2, A3 = B3 FROM A, B WHERE A.ID = B.ID
The writing in Oracle and DB2 is more troublesome, 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)