views:

52

answers:

2

Hi

In bulk insertion only in the last record trigger is executing.

If I bulk insert 10 records the trigger is running only for the 10th record. But I need trigger to run on all 10 records.

Please help me with an example

+2  A: 

I assume you're talking SQL Server? This page gives you some explanation. To enable triggers, you would use a syntax like this:

INSERT ... SELECT * FROM OPENROWSET(BULK...)
David M
+1  A: 

Trigger is executed per statement, not per row. If you assign some variable from a column in inserted or deleted tables, you will get only single, probably last value. The inserted table will contain as many rows as there are in the bulk insert batch. Here's an example with AdventureWorks database:

declare @AddressLine nvarchar(50)
select count(*) [Address count] from Person.Address
select @AddressLine = AddressLine1 from Person.Address
select @AddressLine --only one, last address line
Piotr Rodak
piotr is correct. There are 2 tables which exist implicitly in all triggers called "inserted" and "deleted" . If you insert 10 rows, all 10 rows will be in the hidden table(s). See this article for some more info: http://www.sqlteam.com/article/an-introduction-to-triggers-part-i
Jeremy