Jump to content

Trigger After Update - ERROR 1442 (HY000)


s0c0

Recommended Posts

I'm trying to create a trigger that updates a field each time another field in the same table is changed.  I created a numerical hash of a varchar column for increasing speed on lookups and group by operations.  However, I get the following error:

 

MySQL Trigger: ERROR 1442 (HY000): Cant update table 'tbl' in stored function/trigger because it is already used by statement which invoked this stored function/trigger

 

It looks like MySQL is preventing the trigger from executing because it would create an infinite loop.  Here is my trigger syntax:

 

delimiter $$
CREATE TRIGGER pHash

AFTER UPDATE ON products FOR EACH ROW
BEGIN
UPDATE products SET model_hash = crc32(products_model);
END$$
delimiter ;

 

Any idea on how I could accomplish this?  Doing this in the code is not an option because we have a very old code base where updates occur all over the place, not very OOP at all so this must occur within the database engine.

Link to comment
Share on other sites

Trigger/Function limitation... (I think that I did answer something exactly like this yesterday)... in a trigger/function you can not execute any other SQL sentence that involve the same table used for the trigger/function.... (INSERT,UPDATE, DELETE,  TRUNCATE, DROP, etc..etc)

 

but in your triggers (BEFORE UPDATE, BEFORE INSERT) you can assign/calculate your field model_hash value as you want, no need to use another UPDATE clause... per example in a BEFORE UPDATE you can access the OLD and the NEW values of any field

 

look for examples here: http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx

Link to comment
Share on other sites

Nevermind the last reply.  I have got it working, but the only problem is its doing the calculation for each row on an update, even if only one row is updated. .....

 

That is impossible if your process is updating just one row... the trigger is going to act only over the affected row(s) in whatever update that you are executing... check your update not the trigger.

 

Here is the current syntax:

CREATE TRIGGER pHash
BEFORE UPDATE ON products FOR EACH ROW SET NEW.model_hash = crc32(NEW.products_model);

 

the syntax that you showed in the second time is incorrect..  first one was close ...just modify it slightly  and pay attention to the DELIMITER when you create it (maybe that was the error in your first intent):

 

CREATE TRIGGER pHash
BEFORE UPDATE ON products
FOR EACH ROW 
BEGIN
SET NEW.model_hash = crc32(NEW.products_model);
END

 

 

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.