I have following trigger. On Insertion of a new row, the Stored Procedure is unable to get the parameter value of variable @ItemID. I am trying to pass the value of ItemID Column of newly inserted Row to the stored procedure CalculateCurrentStock
ALTER TRIGGER UpdateCurrentStock
ON StockIn
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
EXEC CalculateCurrentStock (SELECT ItemID From INSERTED)
END
The error text reads
Procedure or function 'CalculateCurrentStock' expects parameter '@ItemID', which was not supplied. The statement has been terminated.
Thank you for help.
EDIT: Answer
I have altered the trigger as per Derek Kromm's and KM's suggestion
ALTER TRIGGER UpdateCurrentStock
ON StockIn
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @CSV varchar(max)
SELECT @CSV=ISNULL(@CSV+',','')+ItemID From INSERTED
EXEC CalculateCurrentStock @CSV
END
Thanks for help :)
editwill fail if yourINSERTEDtable contains more than one row. It is best practice to always write triggers to support multiple rows inINSERTEDand/orDELETED. It is fairly easy and common to insert/update/delete multiple rows in a single statement, and any trigger that only handles a single row will fail in a "logical" manner without an error message (your data gets messed up). try something this to see your trigger fails: INSERT INTO StockIn (col1, col2,...) SELECT TOP 3 col1, col2, ... FROM StockIn ORDER BY ItemID` this assumes ItemID is an identity PK.