SQL Server - 根据条件构造update where子句

时间:2013-03-06 11:26:45

标签: sql-server-2008

请参阅以下查询:

Update Employee
Set AccountManagerId = a.AM_ID
FROM Employee e INNER JOIN AccountManager a on e.Id = a.Id
WHERE

**努力构建以下部分(需要添加到where子句)**

If a.Department is not null then [FOLLOWING NEEDS TO BE ADDED TO WHERE CLAUSE] (e.Department = a.department)

努力将此添加到where子句。因此,如果department不是NULL,则将其添加到WHERE子句

3 个答案:

答案 0 :(得分:0)

你能不能这样做:

Update Employee
Set AccountManagerId = a.AM_ID,
e.Department = a.department
FROM Employee e INNER JOIN AccountManager a on e.Id = a.Id
WHERE
a.Department IS NOT NULL

答案 1 :(得分:0)

为什么不在连接中添加另一个条件?

我相信这将达到您的目的,只更新拥有部门的匹配客户经理的员工。

UPDATE Employee
SET AccountManagerId = a.AM_ID
FROM Employee e 
INNER JOIN AccountManager a 
ON e.Id = a.Id
AND
e.Department = a.Department

答案 2 :(得分:0)

Update Employee
Set AccountManagerId = a.AM_ID
FROM Employee e INNER JOIN AccountManager a on e.Id = a.Id
WHERE ((a.Department IS NULL) || (e.Department = a.department))