在SQL中更新特定列

时间:2014-06-07 15:34:49

标签: sql sql-server tsql

我有四张桌子(简化版)

客户端:

    ClientID
    ManagerID
    UserID
    VolunteerID

经理

    ManagerID
    TeamID

用户

  UserID 

  TeamID

我需要为特定团队更新特定客户端上的用户ID(Client.UserID)

我已经提出了以下内容但它似乎无法正常工作 - 客户端上的用户ID未被更改:

    UPDATE C
    SET C.UserID = 28 
    FROM vTeams T 
    INNER JOIN Vmanagers M ON T.TeamID = M.TeamID
    INNER JOIN dbo.vClients C ON M.ManagerID = C.ManagerID
    INNER JOIN vUsers U ON C.UserID = U.UserID
    WHERE 
    T.TeamID = 251

TIA

3 个答案:

答案 0 :(得分:0)

编辑:这个答案适用于MySQL,但我认为它不适用于SQL Server ...抱歉..我会离开它,以防它对任何人都有帮助

我没有对此进行测试,但UPDATE子句中没有FROM,并且SET位于JOIN之后。尝试这样的事情:

UPDATE dbo.vClients C
    INNER JOIN Vmanagers M ON M.ManagerID = C.ManagerID
    INNER JOIN Team T ON T.TeamID = M.TeamID
#    INNER JOIN vUsers U ON C.UserID = U.UserID
# not sure if you'll need the above join, based on the example
    SET C.UserID = 28 
    WHERE 
    T.TeamID = 251

答案 1 :(得分:0)

我的错误是选择检查更新的错误

答案 2 :(得分:0)

如果您的列是身份表,请尝试将其设置为关闭,然后运行您的脚本,然后将其返回为:

SET IDENTITY_INSERT C ON
    UPDATE C
SET C.UserID = 28 
FROM vTeams T 
INNER JOIN Vmanagers M ON T.TeamID = M.TeamID
INNER JOIN dbo.vClients C ON M.ManagerID = C.ManagerID
INNER JOIN vUsers U ON C.UserID = U.UserID
WHERE 
T.TeamID = 251

SET IDENTITY_INSERT C OFF

或者你可以用AND分隔多个where语句:

UPDATE C
SET C.UserID = 28 
WHERE
(M.TeamID =251
AND
C.ManagerID = 251
AND
U.UserID = 251
AND
T.TeamID = 251)

或者这可以帮助你: SQL update query syntax with inner join