update set from statement usage

Here is an example:
two tables a, b, b, want to make the memofield value is equal to a table corresponding to idthe namevalues of
table a:

id, name  
123

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 whereand setneed to be associated with a table for query, the entire updateexecution requires two scans of the associated table, which is obviously inefficient.
In this case, Sybaseand SQL SERVERthe 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 joinand other) are often used selectstatement, in fact, in SQL syntax, these connections can also be used updateand deletestatements, in these statements joinoften 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!

OralceDB2Syntax 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 Serverthe Updategrammatical 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)

Guess you like

Origin blog.csdn.net/WuLex/article/details/113180133