Quote:
quote:CREATE TRIGGER "bcc.Trigger_UpdateProducts" ON InventoryTransactions
FOR INSERT
Not for replication
AS
UPDATE
dbo.products
SET
dbo.products.UnitsOnOrder=dbo.InventoryTransaction s.UnitsOrdered
FROM
dbo.InventoryTransactions, inserted
where dbo.products.productID=dbo.InventoryTransactions.p roductID
|
Please look your code again, as I see it there, You have used the inserted table only in the FROM clause. But you dont use the same in the WHERE clause, without which the query is meaningless. Going by the code you posted, it would update all the records in PRODUCT table, every time an INSERT event occurs on InventoryTransactions Table. Thats why you get too many rows updated as you said.
If I am not wrong, it should be like this. Every time a record is inserted into InventoryTransactions, the same records ProductId is used as a look up value in Products table and its UnitsOnOrder should get updated. If that is so, then your Update statement should be as follows.
CREATE TRIGGER "bcc.Trigger_UpdateProducts" ON InventoryTransactions
FOR INSERT
Not for replication
AS
UPDATE dbo.products SET
dbo.products.UnitsOnOrder=Inserted.UnitsOrdered
FROM dbo.InventoryTransactions, inserted
where dbo.products.productID=Inserted.productID
Coz, Inserted table is a replica of the record that you are trying to insert.
Also, I noticed another logical error. Everytime the Record is inserted, should the PRODUCTS.UnitsOrdered be incremented? If so, it should look like
UPDATE dbo.products SET
dbo.products.UnitsOnOrder=dbo.products.UnitsOnOrde r + Inserted.UnitsOrdered
FROM dbo.InventoryTransactions, inserted
where dbo.products.productID=Inserted.productID
Hope that helps.
Cheers,
-Vijay G