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

耗尽温柔 提交于 2021-01-07 06:57:48

问题


I am trying to update a table whenever a new row is inserted into another table.

When a row is added to the storeRoutes table, I want this trigger to update a column in my productList table. It should set the column isAvailable to 0 (false) using the storeProductId column from the new row inserted into storeRoutes.

Here is my trigger:

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

    SET NOCOUNT ON;     

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

END

But I keep getting an error:

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

What am I doing wrong?

Thanks!


回答1:


You need to select from pseudo-table inserted:

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

You could also use exists:

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


来源:https://stackoverflow.com/questions/65328245/multi-part-identifier-error-when-trying-to-create-a-trigger-on-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!