1. When using the data in one table to update the data in another table, the T-SQL provides a variety of writing methods (two listed below), but it is recommended to use the first method, although the traditional, but the structure is clear.
Note that when you use data in one table to update data in another table, the two tables must be associated!
1.
Update T1
Set t1.c2 = t2.c2
From T2
Where t1.c1 = t2.c1
2.
Update T1
Set t1.c2 = t2.c2
From T1 inner join T2
On t1.c1 = t2.c1
Example:
UPDATE temp_Org_employee SET temp_Org_employee.mobile = c.tele FROM temp_Org_employee e INNER JOIN (SELECT a.name, b.ID, a.tele, b.mobile FROM EmplTeleTb a LEFT JOIN (SELECT * FROM temp_Org_employee WHERE DepartmentId IN (SELECT ID FROM org_Department WHERE CompanyID = 'T200812051418466710000194')) b ON a.Name = b.EmplName WHERE a.tele <> b.mobile) c ON e.ID = c.ID
Example 2:
UPDATE temp_Org_employee SET temp_Org_employee.mobile = c.tele FROM (SELECT a.name, b.ID, a.tele, b.mobile FROM EmplTeleTb a LEFT JOIN (SELECT * FROM temp_Org_employee WHERE DepartmentId IN (SELECT ID FROM org_Department WHERE CompanyID = 'T200812051418466710000194')) b ON a.Name = b.EmplName WHERE a.tele <> b.mobile) c WHERE temp_Org_employee.ID = c.ID
Example 3:
UPDATE temp_Org_employee SET mobile = c.tele FROM temp_Org_employee e,(SELECT a.name, b.ID, a.tele, b.mobile FROM EmplTeleTb a LEFT JOIN (SELECT * FROM temp_Org_employee WHERE DepartmentId IN (SELECT ID FROM org_Department WHERE CompanyID = 'T200812051418466710000194')) b ON a.Name = b.EmplName WHERE a.tele <> b.mobile) c WHERE e.ID = c.ID
2. the alias of the table specified in the from clause cannot be set
Column_nameThe qualifier of the modified field in the clause.
For example, the following content is invalid:
Update temp_org_employee
Set E. Mobile = C. Tele
From temp_org_employee E, (select a. Name, B. ID, A. Tele, B. Mobile
From emplteletb
Left join
(Select *
From temp_org_employee
Where else mentid in
(Select ID
From org_department
Where companyid = 't20081205141846670000194 '))
B
On a. Name = B. emplname
Where a. Tele <> B. Mobile) c
Where E. ID = C. ID
To make the preceding example legal, delete alias E from the column name or use its own table name.