Sql server 2008 SQL Server-基于条件构造update where子句

Sql server 2008 SQL Server-基于条件构造update where子句,sql-server-2008,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.de

请参阅以下查询:

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子句中。因此,如果部门不为空,那么将此添加到WHERE子句

中,您是否可以不这样做:

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

为什么不向联接添加另一个条件呢

我相信这会达到你的目的,只更新员工与匹配的客户经理谁有一个部门

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

我想他想用AccountManager部门栏来更新员工部门栏我不确定到底发生了什么。如果得到澄清,我将根据需要更新/删除我的答案appropriate@mattytommo-没错!这样做的目的是让我的rep如此之高,以至于会导致堆栈溢出异常:o)@mattytommo-bigint的限制为9223372036854775807-malcolm birtle会对字段使用8字节进行拟合@mattytommo-它在哪里说的?这里它表示最大值为2^63,即9223372036854775807。如果该值变高,则需要为字段分配更多字节,并使用不同的类型:您必须在bigint上调用.ToByteArray(),并将其保存为类似SQL:@mattytomo-lol dooche中的
图像的内容!Int64直接映射到SQL m8中的BigInt
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))