1

On bulk insert, Insert trigger only works for the first record and does not work for all the other records but trigger works properly when records are inserted using cursor.

Insert trigger updates few columns of destination table. To insert bulk data I am using following script

INSERT INTO DestinationTable (Column1, Column2)
SELECT * FROM SourceTable

I get few columns in trigger of inserted record like the following script, and work on them to update columns of DestinationTable

SELECT @col1 = Column1, @col2 = Column2, FROM INSERTED
  1. Why on bulk insert, trigger does not work?
  2. Am I missing something or I have to use cursor?

I am using SQLServer 2005

EDIT

Trigger Code

http://stashbox.org/957108/InsertTrigger.sql

Thanks.

1
  • What does the docs say? IIRC, they say what you experience.
    – leppie
    Jul 19, 2010 at 8:43

3 Answers 3

1

From the code you posted it looks like by bulk insert you just mean inserting multiple rows. Not this BULK INSERT?

The INSERTED pseudo table contains all the rows inserted by the statement. It is not a row level trigger. You would need to use a cursor for RBAR processing or, ideally, process it as a set. For example if you are Updating another table you could join onto the inserted table and update all the rows in one statement.

5
  • Thanks Martin, can you please elaborate this; process it as a set?
    – Kashif
    Jul 19, 2010 at 8:45
  • SourceTable has 10K rows and I want to insert all in DestinationTable in one go and also want Insert Trigger to update each row.
    – Kashif
    Jul 19, 2010 at 8:48
  • I have uploaded trigger script. Please see my edit in question. It would be really great if you mention other mistakes too while going through it.
    – Kashif
    Jul 19, 2010 at 9:50
  • All looks good to me. Was quite a bit longer than I was expecting! Basically in order to get your trigger working against multiple rows you could just use a cursor and process each row in the way you are currently doing or you would need to replace the parts where it is currently populating a scalar variable per row so that it works for multiple rows. Maybe you could begin by using table variables instead of scalar variables then try and combine some of the steps. Jul 19, 2010 at 10:16
  • Thanks Martin. Great help. Really appreciate it.
    – Kashif
    Jul 19, 2010 at 10:20
0

Well on cursor it is because each record is being inserted one at a time and not in a batch. So with bulk insert they insert in a batch. So the trigger fires once for the batch.

I think I read about a workaround one time that was pretty clean. Let me see if I can find it.

EDIT: You know when you said bulk operation I didn't even pay attention to the sql and assumed you were using bcp. But I do still remember a work around that I am going to look for.

EDIT2: Ok take a look at this article and see if it helps you out: http://weblogs.sqlteam.com/tarad/archive/2004/09/14/2077.aspx

1
  • Thanks @spinon, if you can find it that would be great help.
    – Kashif
    Jul 19, 2010 at 8:43
-1

Who said that trigger not worked with bulkinsert or bulkcopy I made it and it worked perfectly

1
  • If someone want to know how exactly we have to code for it ask me.
    – Ankit Rai
    Jul 30, 2015 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.