TRIGGER on newly inserted rows only in SQL Server 2005

How does the trigger fire?  I'm basically after an efficient way to update on a BULK INSERT in SQL Server 2005

i'm doing a BULK INSERT and want to update  the new rows only after the INSERT.

As the table gets larger and larger, i do not want the conditional UPDATE to query the whole destination table of tblCustomer  -  I only want to query the newly inserted rows.

CREATE TRIGGER triggerA ON tblCustomer AFTER INSERT AS
UPDATE tblCustomer
SET xflag = 1
FROM tblCustomer, tblAddress
WHERE tblCustomer.customerID  = tblAddress.customerID

This trigger runs across the whole of tblCustomers

Now the questions i have are
1.  does the bulk insert complete on all rows, before the trigger is fired
2.  how do i only run the UPDATE query against the new rows


charb0yAsked:
Who is Participating?
 
adatheladCommented:
Hi,

1. Yes, the trigger will fire once at the end
2.
CREATE TRIGGER triggerA ON tblCustomer AFTER INSERT AS
UPDATE tblCustomer
SET xflag = 1
FROM tblCustomer
    JOIN tblAddress ON tblCustomer.customerID = tblAddress.customerID
    JOIN inserted on tblCustomer.customerID = inserted.customerID

"inserted" holds details on the new records created
0
 
adatheladCommented:
I wasn't sure if you actually needed the tblAddress join in there, but kept it in just incase (i.e. only update new customers that have an address recorded). If not, then just remove that join
0
 
charb0yAuthor Commented:
fantastic  - exactly what i've been looking for
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.