A, field:
Sdeptkey: Master Region
Sdeptid
Sdeptname: the name of the local device.
Employee table B, field:
Sempkey: Master Region
Sdeptkey: foreign key: A. sdeptkey
Sdeptid
Sempid: employee ID
Sempname: Name
Now B. sdeptkey has expired, and the relationship between B. deptid = A. deptid is required. Modify it.
There are two methods to update SQL in SQL Server:
1. SQL Server-specific explain Method
Update B set sdeptkey = A. sdeptkey from a where a. sdeptid = B. sdepid
2. standard SQL
Update B set sdeptkey = (select sdeptkey from a where a. sdeptid = B. sdeptid)
In SQL Server, the two regions are different:
Region:
The first method is faster, and SQL server has a reverse join (which will take advantage of the index) processing.
The second method is slower. If the value is not found, the system returns NULL and the update value is null.
Region 2:
The data volume of Table B is as follows:
Sempkey, sdeptkey, sdeptid...
---------------------------
001,789, A01...
002,456, A01...
003,002, A02...
004,003, a03...
---------------------------
The data in table A is as follows:
Sdeptkey, sdeptid...
---------------------------
001, A01
---------------------------
After the first update SQL statement is executed in rows, the result of Table B is as follows:
Sempkey, sdeptkey, sdeptid...
---------------------------
001,001, A01...
002,001, A01...
003,002, A02...
004,003, a03...
---------------------------
After the second update SQL statement is executed in rows, the result of Table B is as follows:
Sempkey, sdeptkey, sdeptid...
---------------------------
001,001, A01...
002,001, A01...
003, null, A02...
004, null, a03...
---------------------------
That is to say,
Update B set sdeptkey = A. sdeptkey from a where a. sdeptid = B. sdepid
When using the SQL standard syntax, you should add a where clause where exists (select * from a where a. sdeptid = B. sdeptid), that is:
Update B set sdeptkey = (select sdeptkey from a where a. sdeptid = B. sdeptid)
Where exists (select * from a where a. sdeptid = B. sdeptid)
In Oracle, the update SQL statement of the benchmark is not the same as that of SQL Server.