问题
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