about triggers and bulk insert

  • Hi

    I have a query regarding trigger

    I have a table Table1, and on this i have created a trigger Trg1 which will fire when we insert a row into Table1. If i want to perform bulk data insert into Table1, i will disable the trigger Trg1 and then i will do the bulk insert. and enable the trigeer.

    consider a scenario where the Trigger trg1 will copy(replicate) the newly inserted data from Table1 to another table Table2.

    Now please let me know how can we perform this replication of data for the bulk inserted rows

  • Hi,

    Are these tables part of a SQL Server replication? If not, and if it is a true copy operation, why can't you BULK INSERT to the other table as well?

    /Elisabeth

    elisabeth@sqlserverland.com
    MCITP | MCT
    http://sqlblog.com/blogs/elisabeth_redei/
    http://linkedin.com/in/elisabethredei

  • Hi Thanks for your reply.

    This is not part of SQL Replication.

    The code in the trigger is not just copying the newly added rows. it involves some validations and then copying of newly added rows to another table.

    my question is, how to run the trigger code for the newly inserted data?

  • Hi,

    Well the trigger by definition will trigger on some action so you will have to break out the code and put it in a (e.g.) stored procedure and then find some means of identifying the rows you just inserted.

    If your goal is to keep the BULK INSERT operation as fast as possible, you could also BULK INSERT into an empty temporary table (not #temp table), and then run whatever code you wish on that table; inserting into both of your tables at the same time.

    /Elisabeth

    elisabeth@sqlserverland.com
    MCITP | MCT
    http://sqlblog.com/blogs/elisabeth_redei/
    http://linkedin.com/in/elisabethredei

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply