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
https://forums.phpfreaks.com/topic/247445-trigger-help/
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
https://forums.phpfreaks.com/topic/247445-trigger-help/#findComment-1270721
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
https://forums.phpfreaks.com/topic/247445-trigger-help/#findComment-1270733
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.