2013-03-06 34 views
1

請參考下面的查詢:SQL服務器 - 建築更新where子句根據病情

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子句。所以,如果部門不爲NULL,然後加入這WHERE子句

回答

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)) 
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 
+0

去爲20K現在呢? ;) – mattytommo 2013-03-06 11:36:57

+0

@mattytommo 100萬K;) – 2013-03-06 11:39:24

+0

@mattytommo - 正好!目標是讓我的代表如此之高,這將導致一個堆棧溢出異常:o) – 2013-03-06 11:41:19

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 
+0

我想''他試圖用AccountManager部門更新員工部門列列 – 2013-03-06 11:31:59

+0

我不確定到底發生了什麼。如果澄清,我會根據情況更新/刪除我的答案 – 2013-03-06 11:36:28