SQL Server - 从另一个表更新数据

时间:2016-11-21 12:37:49

标签: sql sql-server

我的问题是:

我有2个表分配服务 我试图从另一个表的列数据中更新一列。

UPDATE [Services]
SET
      TeamID = (SELECT [AssignedTeam]
                       FROM [Assignment]
                       WHERE [ServiceName] in ([Services].[ServiceName]))
WHERE
    EXISTS (
        SELECT [ServiceName]
        FROM [Assignment])

它不起作用。有人可以帮我解决这个问题吗? THX

2 个答案:

答案 0 :(得分:3)

使用JOIN更新。

<强>查询

UPDATE t1
SET t1.[TeamID] = t2.[AssignedTeam]
FROM [Services] t1
JOIN [Assignment] t2
ON t1.[ServiceName] = t2.[ServiceName];

答案 1 :(得分:1)

UPDATE [Services] SET TeamID = [AssignedTeam] FROM [Assignment] WHERE [Assignment].[ServiceName] = [Services].[ServiceName]