在一个SQL查询中更新两个表

时间:2015-02-20 16:55:44

标签: sql sql-server

我正在尝试从一个SQL查询更新我的ScoutScoutRole表。我试过这个例子:How to update two tables in one statement in SQL Server 2005?

但我一直收到错误消息

  

无法绑定多部分标识符“SR.Role”

我该如何解决这个问题?

BEGIN TRANSACTION

UPDATE Scout
SET Scout.FirstName = @ScoutFirstName, 
    Scout.LastName = @ScoutLastName, 
    Scout.EmailAddress = @EmailAddress,
    Scout.ClubID = @ClubID
FROM Scout S, ScoutRole SR
WHERE S.ScoutID = SR.ScoutID AND S.ScoutID = @ScoutID

UPDATE ScoutRole
SET SR.Role = @ScoutRole,
    SR.Username = @Username,
    SR.Password = @Password
FROM Scout S, ScoutRole SR
WHERE S.ScoutID = SR.ScoutID AND S.ScoutID = @ScoutID

COMMIT

1 个答案:

答案 0 :(得分:1)

这应该是您需要使用的全部内容:

BEGIN TRANSACTION

UPDATE Scout
SET FirstName = @ScoutFirstName, 
    LastName = @ScoutLastName, 
    EmailAddress = @EmailAddress,
    ClubID = @ClubID
WHERE ScoutID = @ScoutID

UPDATE ScoutRole
SET Role = @ScoutRole,
    Username = @Username,
    Password = @Password
WHERE ScoutID = @ScoutID

COMMIT