尝试在插入时创建触发器时出现多部分标识符错误

Multi-part identifier error when trying to create a trigger on insert

每当将新行插入另一个 table 时,我都会尝试更新 table。

将行添加到 storeRoutes table 时,我希望此触发器更新我的 productList table 中的列。它应该使用插入 storeRoutes.

的新行中的 storeProductId 列将列 isAvailable 设置为 0(假)

这是我的触发器:

CREATE TRIGGER [setIsAvailableFalse]
    ON [storeRoutes]    
    AFTER INSERT
    AS  BEGIN

    SET NOCOUNT ON;     

    UPDATE productList
    SET isAvailable = 0     
    WHERE productId = Inserted.storeProductId

END

但我一直收到错误消息:

multi-part identifier, 'Inserted.storeProductId' could not be bound.

我做错了什么?

谢谢!

你需要select从伪tableinserted:

UPDATE pl 
SET isAvailable = 0     
FROM productList pl
INNER JOIN Inserted i ON pl.productId = i.storeProductId

您也可以使用 exists:

UPDATE pl 
SET isAvailable = 0     
FROM productList pl
WHERE EXISTS (SELECT 1 FROM Inserted i WHERE  pl.productId = i.storeProductId)