I tried creating a trigger to store the data manipulations done on my Food table inside another table called FoodTriggerTable. When I insert only one row of data, I can record the data manipulation well in FoodTriggerTable. However, when I insert multiple rows at once, I only store the information of the first row of data. I have the following trigger:
CREATE TRIGGER InsertFoodTrigger ON Food
AFTER INSERT
AS
DECLARE @FoodID varchar(5);
DECLARE @FoodName nvarchar(50);
DECLARE @FoodDesc nvarchar(200);
DECLARE @FoodPrice money;
DECLARE @InsertAction varchar(200);
DECLARE @InsertActionTime datetime;
DECLARE @Amount int;
SELECT @FoodID = i.FoodID FROM INSERTED i;
SELECT @FoodName = i.FoodName FROM INSERTED i;
SELECT @FoodDesc = i.FoodDesc FROM INSERTED i;
SELECT @FoodPrice = i.FoodPrice FROM INSERTED i;
SELECT @Amount = COUNT(*) FROM INSERTED i;
SET @InsertAction = 'You''ve inserted ' + CONVERT(varchar(10), @Amount) + ' data into ''Food'' table';
INSERT INTO FoodTriggerTable (FoodID, FoodName, FoodDesc, FoodPrice, InsertAction, InsertActionTime)
VALUES (@FoodID, @FoodName, @FoodDesc, @FoodPrice, @InsertAction, GETDATE());
GO
And I tried the following INSERT:
INSERT INTO Food
VALUES ('CH006', 'Dummy Data 1', 'Dummy data', '0', '34'),
('CH007', 'Dummy Data 2', 'Dummy data', '0', '75');
How do I fix my trigger so that it also records the second row of data manipulation?