2012-09-24 87 views
2

我有兩個表;SQL Server 2005更新與查詢表查詢

技術查找表

OSID OSValue 

1  UNIX    
2  ORACLE    
3  .NET 
4  SQL  
5  SAP 

------------------ 

員工技術

empID OSID 

34  1 
37  2 
45  2 
34  4 
45  6 

員工ID號37是不使用Oracle。他轉移到SAP。那就是OSID應該在第二個表中從2更新到5。

我有了技術和員工ID瓦萊斯一個臨時表

請幫我 - 編寫連接兩個以上表的查詢 - 查找SAP技術的ID,並更新第二帶有該ID的表格。

Update emp 
Set emp.OSID = (Select OSID from technologylookup where OSValue = tmptbl.technologyname) 
FROM employee_tech emp INNER JOIN #Temptbl tmptbl ON emp.emp_id = tmptbl.emp_id 

回答

3

加入您的查找表,臨時表,然後從該表

Update emp 
    Set emp.OSID = tl.OSID 
FROM employee_tech emp 
INNER JOIN #Temptbl tmptbl 
    ON emp.emp_id = tmptbl.emp_id 
INNER JOIN technologylookup tl 
    ON tl.OSValue COLLATE DATABASE_DEFAULT = tmptbl.technologyname 
+0

感謝您的幫助。您的答案似乎正確,但出現以下錯誤 – user1681514

+0

無法解決等於操作中「Latin1_General_CI_AI」和「SQL_Latin1_General_CP1_CI_AS」之間的排序規則衝突。 – user1681514

+0

COLLATE DATABASE_DEFAULT在連接 – William

0

從我腦袋裏的,未經測試的更新與ID的employee_tech表:

update employee_tech 
set osid = t.osid 
from #Temptbl tmp 
inner join employee_tech e on e.emp_id = tmp.emp_id 
inner join technologylookup t on t.osvalue = tmp.technologyname 
0

如果'臨時表'中的'技術'等於ID,您可以像這樣更新。

UPDATE employee_tech 
SET OSID = (SELECT technology FROM #Temptbl AS T INNER JOIN employee_tech AS E ON T.empID = E.empID)