Insert New Record to the tblPurchase Table Update tblProductStockTable Quantity
07:51 19 Jan 2021

I have table called tblPurchase and tblProductStock. When I insert new record to the tblPurchase table I want to update my tblProductStock table. `

Alter TRIGGER [dbo].[trig_passQuantity_toProductStock] 
ON [dbo].[tblPurchase]
AFTER INSERT 
AS
BEGIN  
    SELECT MAX(ItemName) ItemName,
    ItemCode,
    SUM(Quantity) Quantity  
    FROM tblPurchase
    GROUP BY itemCode;

    UPDATE tblProductStock
    SET Quantity = tblProductStock.Quantity
    FROM tblPurchase WHERE tblProductStock.ItemCode = tblPurchase.ItemCode

    INSERT INTO tblProductStock
    (
        ItemName,
        ItemCode,
        Quantity
    )
    SELECT  ItemName, ItemCode, Quantity FROM tblPurchase WHERE ItemCode NOT IN (SELECT ItemCode FROM tblProductStock)
END

I used above trigger to update and inset data. insert is working as expected. but Update query is not working. This is my both tables

In here I add Dell Laptop twice. But In my tblProductStock table did not change to 110. still showing 50. How can I edit this Update query.

c# sql sql-server