一起更新联接表会导致 "multi-part identifier could not be bound"

Updating joined tables together results in "multi-part identifier could not be bound"

我目前正在尝试在一个查询中更新两个相关表的数据。虽然由于数据冗余,这可能不是最好的主意,但我想避免在我的应用程序中对更改的数据进行大量检查。

这些是正在使用的表格:

Process (Id, Name, Description)
FavoriteProcess (Id, Process_Id, Position, CreatedTime)

如您所见,每个 FavoriteProcess 实体都与一个 Process 相关,并且一个 Process 可以出现在多个 FavoriteProcess 实体中。

我正在编写一个工具,可以用来更新 FavoriteProcess 实体的 Position 属性 和相关实体的 Description 属性 Process 还有。对于这个问题,我编写了以下发出错误的存储过程:

CREATE PROCEDURE UpdateFavoriteProcess
    -- parameters for the stored procedure
    @Id                     int,
    @ProcessDescription     varchar(256),
    @Position               int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- The update query
    UPDATE fp
    SET p.Description = @ProcessDescription, -- this line is issuing the error
        fp.Position = @Position
    FROM FavoriteProcess AS fp
    INNER JOIN Process AS p ON fp.Process_Id = p.Id
    WHERE fp.Id = @Id
END
GO

查询发出此错误:

Msg 4104, Level 16, State 1, Procedure UpdateFavoriteProcess, Line 13 The multi-part identifier "p.Description" could not be bound.

我已尝试搜索此问题,但 5 个相关 SO 问题中的 none 提供了可以解决我的问题的解决方案!我试过了...

但是 none 我的尝试成功了。所以我真的很感谢能帮我解决这个问题的人!

不能同时更新两个表

当您使用

开始更新时
UPDATE fp

可以

SET p.Description 

这就是您收到 Not Bound 错误的原因。

您可以使用两个单独的更新语句:

UPDATE FavoriteProcess
SET Position = @Position 
WHERE Id = @Id

UPDATE Process  
SET Description = @ProcessDescription
WHERE Id IN (SELECT Process_Id FROM FavoriteProcess WHERE Id = @Id)