更新语句将更新"活动"如果相应部门处于非活动状态,则为员工的字段

时间:2018-05-29 03:51:24

标签: sql sql-server

我想更新我的员工Employee_Type:如果Department_TypeInactive,那么Employee_Type必须更新为Inactive }。

这是我的代码,我不知道为什么不工作。 tblEmployees中的主键为EmployeeIDtblDepartment中还有一个外键EmployeeID

UPDATE tblEmployees
SET Employee_Type = t2.Department_Type
FROM dbo.tblEmployees AS t1
RIGHT JOIN dbo.tblDepartment AS t2 ON t1.Employee_Type = t2.Department_Type
WHERE t2.EmployeeID = '6';

结果是:

(1 row(s) affected)

但实际上没有任何事情发生。

3 个答案:

答案 0 :(得分:1)

您的书面逻辑意味着以下查询:

UPDATE tblEmployees
SET Employee_Type = t2.Department_Type
FROM dbo.tblEmployees AS t1
RIGHT JOIN dbo.tblDepartment AS t2
    ON t1.Employee_Type = t2.Department_Type
WHERE
    t2.Department_Type = 'inactive'
 -- t2.EmployeeID = '6';    -- not sure about this requirement

我不知道您在WHERE子句中针对特定员工的原因,但这与将更新的所有员工类型设置为非活动状态(其部门也处于非活动状态)不一致。

答案 1 :(得分:0)

试试这个

update t1
set t1.Employee_Type = t2.Department_Type
from dbo.tblEmployees as t1
right join dbo.tblDepartment as t2
on t1.Employee_Type = t2.Department_Type
where t2.Department_Type = 'InActive'

这会将所有员工类型更新为InActive

答案 2 :(得分:0)

我找到了解决问题的方法。

Sub EMAmakeAFunction()
'''''These can be changed as you need
Dim numberOrRowsToSkip As Integer: numberOFRowsToSkip = 70
Dim TrialsToExecute As Integer: TrialsToExecute = 1000
Dim firstFormula As Range: Set firstFormula = Range("D165")
Dim secondFormula As Range: Set secondFormula = Range("D166")
''''Rest of code should be pretty stable of above assumptions are correct.

Dim i As Integer

For i = 1 To TrialsToExecute
    firstFormula.Offset(i * numberOFRowsToSkip).FormulaR1C1 = _
        firstFormula.FormulaR1C1
    secondFormula.Offset(i * numberOFRowsToSkip).FormulaR1C1 = _
        secondFormula.FormulaR1C1

Next i

End Sub