I have the following trigger:
ALTER TRIGGER .[dbo].[trgAfterInsertComment]
ON .[dbo].[Comment]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @Id int;
declare @LoanId int;
declare @CopyId int;
declare @CustomerId int;
declare @Comment nvarchar(255);
--DECLARE cur CURSOR FOR
select @Id = Id from inserted
select @LoanId = LoanId from inserted
--select @CopyId = CopyId from deleted
--select @CustomerId = CustomerId from deleted
select @Comment = Comment from inserted
-- OPEN cur
--FETCH NEXT FROM cur INTO @Id, @ISBN, @Purchase_Date
--WHILE @@FETCH_STATUS = 0 BEGIN
-- your business logic
Declare @Title nvarchar(255);
select @Title = (Select Title from Book where ISBN = (select ISBN from copy where Id = (select CopyId from Loan where Id = @LoanId)))
select @CustomerId = (Select CustomerId from Loan where Id = @LoanId)
select @CopyId = (Select CopyId from Loan where Id = @LoanId)
insert into Activity("Heading", "Date")
values(Concat('New Comment added - Id: ', @Id, ' Title: ', @Title, ' Copy Id: ', @CopyId, ' Customer Id: ', @CustomerId, ' Comment: ', @Comment), GETDATE())
--FETCH NEXT FROM cur INTO @Id, @ISBN, @Purchase_Date
--END
--CLOSE cur
--DEALLOCATE cur
end
As you can see I have commented out a cursor that I was using to handle multiple inserts. Could someone tell me how I can handle multiple inserts without the cursor, as after reading around I see that using a cursor is a bad idea?
With the above trigger, if I try to insert multiple lines like this:
USE [Library]
GO
INSERT INTO [dbo].[Comment]
([LoanId]
,[Comment])
VALUES
(47, 'test'),
(48, 'test'),
(50, 'test')
GO
Only the first row is inserted into my Activity table. Thanks for any help
inserted.