I want to update several tables and values after pasting the values into one table in order to create a trigger. It works great for inserting one row, but as soon as I insert more rows, SQL Server gives me the following error:
Subqueryreturns more than 1 value. is this unacceptable when a subquery follows = or when a subquery is used as an expression?
Here is my trigger:
CREATE TRIGGER [dbo].[tbl_Sales_ForInsert]
ON [dbo].[SALES]
FOR INSERT
AS
BEGIN
DECLARE @ITEMMODEL varchar(100)
SELECT @ITEMMODEL = ITEM_MODEL FROM inserted
UPDATE SALES
SET PROFIT = TOTAL_PRICE - (SELECT QUANTITY FROM SALES WHERE ITEM_MODEL = @ITEMMODEL) * (SELECT RATE FROM ITEM_DETAILS WHERE ITEM_MODEL = @ITEMMODEL)
WHERE ITEM_MODEL = @ITEMMODEL
UPDATE ITEM_DETAILS
SET QUANTITY = QUANTITY - (SELECT QUANTITY FROM SALES WHERE ITEM_MODEL = @ITEMMODEL)
WHERE ITEM_MODEL = @ITEMMODEL
END
The SALESfirst time I insert data into a table , the update was successful, but the second time it causes the error above. ITEM_MODEL- foreign key constraint in the SALES table.
I suffer from this error, can someone help me?