我需要帮助创建此触发器,任何帮助都会欣赏它,我无法保存此脚本,它说'无效列名OfferId'。但我检查了所有列的名称,它们是正确的
CREATE TRIGGER dbo.OrderOffer_UpdatedUnits
ON dbo.OrderOffer
FOR UPDATE
AS
BEGIN
DECLARE @OfferId char(5) SET @OfferId = (Select OfferId From INSERTED i, OrderOffer a Where i.OrderOfferid = a.OrderOfferid);
DECLARE @UnitsAvailable int SET @UnitsAvailable = (Select SUM(UnitsAvailable) From dbo.Offer Where OfferId=@OfferId);
UPDATE dbo.OrderOffer SET dbo.UnitsAvailable = @UnitsAvailable
FROM INSERTED i, OrderOffer a
WHERE i.OrderOfferid = a.OrderOfferid
END
答案 0 :(得分:0)
试试这个:
CREATE TRIGGER dbo.OrderOffer_UpdatedUnits
ON dbo.OrderOffer
FOR UPDATE
AS
BEGIN
UPDATE a
SET dbo.UnitsAvailable =
(
Select SUM(b.UnitsAvailable)
From dbo.Offer b
Where b.OfferId = a.OfferId
)
FROM OrderOffer a
INNER JOIN INSERTED i
ON i.OrderOfferid = a.OrderOfferid
END
注意:我删除了变量,因为这会假设您一次只更新了一行。即使更新运行多行,上述内容仍然可以正常工作。