Jump to content

Trigger Help


thomasw_lrd

Recommended Posts

Hello,

 

I'm a trying to create a trigger to divide two fields and update them.  It keeps failing with a

 

ERROR 1442: Can't update table 'transactions' in stored function/trigger because it is already used by statement which invoked this stored function/trigger

 

I've read where you can do this with new.fieldname.  Which I have, any ideas?

 

Here is what I have

CREATE
DEFINER=`access`@`%`
TRIGGER `portal_sosha`.`testTrigger`
AFTER UPDATE ON `portal_sosha`.`transactions`
FOR EACH ROW
UPDATE transactions SET NEW.tr_transaction_pct_paid_to_company = tr_transaction_dol_paid_to_ngfg/tr_premium_or_basis_amount;

Link to comment
Share on other sites

the error message is telling you exactly what the problem is:

 

ERROR 1442: Can't update table 'transactions' in stored function/trigger because it is already used by statement which invoked this stored function/trigger..

 

that is one of the known limitations of stored functions & triggers

Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger."

 

http://dev.mysql.com/doc/refman/5.0/en/stored-program-restrictions.html

Link to comment
Share on other sites

the answer depend on what exactly are you trying to do.... if you want to update a field based on the NEW values of other fields... then yes you can... but don't use the  UPDATE clause...

 

example to test:

 

CREATE
DEFINER=`access`@`%`
TRIGGER `portal_sosha`.`testTrigger`
BEFORE UPDATE ON `portal_sosha`.`transactions`   // to calculate the field BEFORE the update (prevent to execute the trigger more than 1 time)
FOR EACH ROW
    SET NEW.tr_transaction_pct_paid_to_company = NEW.tr_transaction_dol_paid_to_ngfg / NEW.tr_premium_or_basis_amount;

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.