0

如何将departmentID属于department代码的所有员工更新500为部门代码503

**tHR员工

> EmployeeId #.......Employeee.........DepartmentID
...101...............Ajith.............101
...102...............Arathy ...........Null
...103...............Arurna............102
...104...............Ambily............101
...105...............Anjaly............Null
...106...............Babitha...........103

**人力资源部

 DepartmentID #.............Code
 101........................500
 102........................501
 103........................502
 105........................503
 ..
4

3 回答 3

1
DECLARE @CodeFrom AS INT
DECLARE @CodeTo AS INT

SET    @CodeFrom = 500 
SET    @CodeTo= 503

UPDATE tHREmployee
SET    DepartmentID = (
                          SELECT DepartmentID 
                          FROM   tHRDepartment 
                          WHERE  Code = @CodeTo
                      )
FROM   tHREmployee E
       JOIN tHRDepartment D
       ON E.DepartmentID  = D.DepartmentID 
WHERE  D.Code = @CodeFrom
于 2013-08-21T07:15:36.943 回答
0

那这个呢:

 Declare @NewDepID int
 Select   @NewDepID = DepartmentID from Departments Where DepartmentCode = 503

 update tHREmployee 
 Set DepartmentID = @NewDepID
 Where DepartmentID in (Select DepartmentID from Departments Where DepartmentCode = 500)
于 2013-08-21T07:09:15.667 回答
0

更新 tHREmployee SET DepartmentID = 105 WHERE DepartmentID = 101;

于 2013-08-21T07:11:29.560 回答