The most common update syntax is:
Update <table_name>
Set <column_name1 >=< value>, Set <column_name2 >=< value>
If my updated value is taken out of a SELECT statement and there are many columns, It is very troublesome to use this syntax.
First, you need to select and place it on a temporary variable. There are many
Second, assign values to the variables.
It is very troublesome to add multiple columns. Can I insert the results of the entire SELECT statement like insert? As if the following
Insert into Table1
(C1, C2, C3)
(Select V1, V2, V3 from table2)
The answer is yes. The specific syntax is as follows:
Update <table_name> <alias>
Set
( <Column_name>, <column_name>
) =
(
Select
( <Column_name>, <column_name>
)
From <table_name>
Where <alias. column_name >=< alias. column_name>
)
Where <column_name> <condition> <value>;
The following is an example:
To make the memo field value in Table B equal to the name value of the corresponding ID in Table
Table A: Id, name
1 Wang
2 Li
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, during the entire update execution, the associated table needs to be scanned twice, which is obviously less efficient.
In this case, the solution for Sybase and SQL Server is to use the update... set... from... where... syntax, which is actually to get the updated data from the source table.
In SQL, table join (left join, right join, inner join, etc.) is often used in select statements. In SQL syntax, these connections can also be used in update and delete statements, in these statements, using join results often get twice the result with half the effort.
UpdateT_orderformSetT_orderform. Sellerid = B. L _ tuserid
From t_orderformLeft join t_productinfo BOn B. L _ id = A. productid
Used to synchronize data from two tables!
Both oralce and DB2 support the following syntax:
Update A set (A1, A2, A3) = ( select b1, b2, b3 from B where . ID = B. ID)
Ms SQL server does not support such a syntax, the corresponding syntax is:
Update A Set A1 = B1, A2 = B2, A3 = B3 From A Left Join B On A. ID = B. ID
I personally feel that the update Syntax of ms SQL Server is more powerful. Ms SQL Server writing:
Update A Set A1 = B1, A2 = B2, A3 = B3 From A, B Where A. ID = B. ID
Writing in Oracle and DB2 is 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)