views:

251

answers:

4

Hello all,

I want to do some calculations when my table data is changed. However, I am updating my table manually and copy pasting about 3000 rows in once. That makes my trigger work 3000 times, but I want it to do the trigger only once.

Is there a way to do that ?

Thanks.

+5  A: 

What's your statement?

If you have multiple inserts then it will fire for each insert you are running. If you want it to execute only once for multiple inserts you must:

  1. Write your insert on a single statement such as insert into foo select * from bar
  2. Your trigger can't be for each row

Other possibility might be:

  1. Disable the trigger
  2. Perform your insertions
  3. Put the trigger code in a stored procedure
  4. Run your stored procedure
Pablo Santa Cruz
A: 

Your trigger will not fire 3000 times if you are modifying 3000 rows in a single statement. Your trigger will fire once and there will be 3000 rows in your virtual 'deleted' table.

Randy Minder
I think by 'manually' the OP may mean he is copying and pasting into some UI tool like an Access Grid or something like that, in which case the tool may be issuing one insert statement per row...
Charles Bretana
+1  A: 

The issue is caused because you are manually pasting the 3000 rows. You really have 2 solutions. You can turn off the trigger by doing this:

ALTER TABLE tablename DISABLE TRIGGER ALL 
-- do work here 
ALTER TABLE tablename ENABLE TRIGGER ALL

and then run the contents of your trigger at then end or you can put your 3000 columns into a temp table and then insert them all at once. This will only setup 1 trigger. If this isn't enough please give us more info on what you are trying to do.

RandomBen
Well I didn't get your first way of solution clearly. You say that "do work here", but I am doing the work manually ? What do you mean, I think I got you wrong. Btw I am using access when copy pasting.
stckvrflw
I am saying run ALTER TABLE tablename DISABLE TRIGGER ALL , then paste in your rows, then run ALTER TABLE tablename ENABLE TRIGGER ALL. Once that is done you will need to run the script that is in your trigger if you want all of those rows to be updated.
RandomBen
+1  A: 

If by 'manually' you mean you are copying and pasting into some User Interface tool (like an Access dataGrid) or something like that, then the tool may be issuing one insert statement per row, and in that case you are out of luck the database trigger will be executed once per insert statement. As other answers have mentioned, if you can insert the rows directly into the database, using a single insert statement, then the trigger will only fire once.

Charles Bretana
yep I am using access
stckvrflw
Yes, as I thought... then I'm afraid that as long as you stick with doing this manually, through Access tableview (or whatever that's called) - you are stuck.
Charles Bretana