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

MysqlTriggers

Mysql Problem Overview


I am running a MySQL Query. But when a new row is added from form input I get this error:

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

From the code:

CREATE TRIGGER `capital` AFTER INSERT ON `brandnames`
FOR EACH
ROW UPDATE brandnames
SET bname = CONCAT( UCASE( LEFT( bname, 1 ) ) , LCASE( SUBSTRING( bname, 2 ) ) )

What does this error mean?

Mysql Solutions


Solution 1 - Mysql

You cannot change a table while the INSERT trigger is firing. The INSERT might do some locking which could result in a deadlock. Also, updating the table from a trigger would then cause the same trigger to fire again in an infinite recursive loop. Both of these reasons are why MySQL prevents you from doing this.

However, depending on what you're trying to achieve, you can access the new values by using NEW.fieldname or even the old values --if doing an UPDATE-- with OLD.

If you had a row named full_brand_name and you wanted to use the first two letters as a short name in the field small_name you could use:

CREATE TRIGGER `capital` BEFORE INSERT ON `brandnames`
FOR EACH ROW BEGIN
  SET NEW.short_name = CONCAT(UCASE(LEFT(NEW.full_name,1)) , LCASE(SUBSTRING(NEW.full_name,2)))
END

Solution 2 - Mysql

The correct syntax is:

FOR EACH ROW SET NEW.bname = CONCAT( UCASE( LEFT( NEW.bname, 1 ) )
                                   , LCASE( SUBSTRING( NEW.bname, 2 ) ) )

Solution 3 - Mysql

A "BEFORE-INSERT"-trigger is the only way to realize same-table updates on an insert, and is only possible from MySQL 5.5+. However, the value of an auto-increment field is only available to an "AFTER-INSERT" trigger - it defaults to 0 in the BEFORE-case. Therefore the following example code which would set a previously-calculated surrogate key value based on the auto-increment value id will compile, but not actually work since NEW.id will always be 0:

create table products(id int not null auto_increment, surrogatekey varchar(10), description text);
create trigger trgProductSurrogatekey before insert on product
for each row set NEW.surrogatekey = 
  (select surrogatekey from surrogatekeys where id = NEW.id);

Solution 4 - Mysql

I have the same problem and fix by add "new." before the field is updated. And I post full trigger here for someone to want to write a trigger

DELIMITER $$

USE `nc`$$

CREATE
    TRIGGER `nhachung_province_count_update` BEFORE UPDATE ON `nhachung` 
    FOR EACH ROW BEGIN
	
	DECLARE slug_province VARCHAR(128);
	DECLARE slug_district VARCHAR(128);

	IF old.status!=new.status THEN  /* neu doi status */
		IF new.status="Y" THEN
			UPDATE province SET `count`=`count`+1 WHERE id = new.district_id;
		ELSE 
			UPDATE province SET `count`=`count`-1 WHERE id = new.district_id;
		END IF;
	ELSEIF old.province_id!=new.province_id THEN /* neu doi province_id + district_id */
	
		UPDATE province SET `count`=`count`+1 WHERE id = new.province_id; /* province_id */
		UPDATE province SET `count`=`count`-1 WHERE id = old.province_id;
		UPDATE province SET `count`=`count`+1 WHERE id = new.district_id; /* district_id */
		UPDATE province SET `count`=`count`-1 WHERE id = old.district_id;
		
		SET slug_province = ( SELECT slug FROM province WHERE id= new.province_id LIMIT 0,1 );
		SET slug_district = ( SELECT slug FROM province WHERE id= new.district_id LIMIT 0,1 );
		SET new.prov_dist_url=CONCAT(slug_province, "/", slug_district);
		
	ELSEIF old.district_id!=new.district_id THEN 
	
		UPDATE province SET `count`=`count`+1 WHERE id = new.district_id;
		UPDATE province SET `count`=`count`-1 WHERE id = old.district_id;
		
		SET slug_province = ( SELECT slug FROM province WHERE id= new.province_id LIMIT 0,1 );
		SET slug_district = ( SELECT slug FROM province WHERE id= new.district_id LIMIT 0,1 );
		SET new.prov_dist_url=CONCAT(slug_province, "/", slug_district);
		
	END IF;
	
	
    END;
$$

DELIMITER ;

Hope this help someone

Solution 5 - Mysql

@gerrit_hoekstra wrote: "However, the value of an auto-increment field is only available to an "AFTER-INSERT" trigger - it defaults to 0 in the BEFORE-case."

That is correct but you can select the auto-increment field value that will be inserted by the subsequent INSERT quite easily. This is an example that works:

CREATE DEFINER = CURRENT_USER TRIGGER `lgffin`.`variable_BEFORE_INSERT` BEFORE INSERT 
ON `variable` FOR EACH ROW
BEGIN
    SET NEW.prefixed_id = CONCAT(NEW.fixed_variable, (SELECT `AUTO_INCREMENT`
												FROM  INFORMATION_SCHEMA.TABLES
												WHERE TABLE_SCHEMA = 'lgffin'
												AND   TABLE_NAME   = 'variable'));		
END

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMitesh MyneeView Question on Stackoverflow
Solution 1 - MysqlSteveView Answer on Stackoverflow
Solution 2 - MysqlrsanchezView Answer on Stackoverflow
Solution 3 - Mysqlgerrit_hoekstraView Answer on Stackoverflow
Solution 4 - MysqlBảo NamView Answer on Stackoverflow
Solution 5 - Mysqlrf1234View Answer on Stackoverflow