Solution for Can’t update table ‘t1′ in stored function/trigger because it is already used by statement which invoked this store

In recently project, i needed to fix a bug about database update. The best way to solve this problem is create an update trigger, but when finished this trigger desiger, a problem show up which the error description is "Can’t update table ‘t1′ in stored function/trigger because it is already used by statement which invoked this stored function/trigger.". So after searched the answer from website, i got a solution about this issue.The following is a sample to show how to fix this problem.

This is a sample table:


CREATE TABLE `t1`

( `a` char(1) default NULL, `b` smallint(6) default NULL );

insert into t1 values ('y','1');

I have a table t1 which has column a and b, i want column a to be updated "n" when column b=0.Here is the fist version i created:


DELIMITER |

CREATE TRIGGER trigger1 AFTER UPDATE ON t1 FOR EACH ROW UPDATE t1 SET a= 'n' WHERE b=0;

| DELIMITER ;

After executed this trigger succesfully, i got an error when i start to update this table.The error statement is "Can't update table 't1' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.".

The following trigger code is a good way:


drop trigger trigger1;

DELIMITER |

CREATE TRIGGER trigger1

BEFORE UPDATE ON t1

FOR EACH ROW

BEGIN IF NEW.b=0 THEN SET NEW.a = 'n';

END IF; END

| DELIMITER ;

Now you can update the table t1. After got this solution i use it on my project.This is the update trigger i wrote:

DELIMITER $$

DROP TRIGGER ModifyLastUpdated

create trigger ModifyLastUpdated BEFORE UPDATE on EMPLOYEE_SKILL for each row

BEGIN if

old.eid=new.eid and old.skill_id=new.skill_id

then set new.last_updated=current_timestamp;

end if;

END;

$$ DELIMITER ;

 

你收到的错误信息 "Can't update table 'reserve' in stored function/trigger because it is already used by statement which invoked this stored function/trigger." 意味着你不能在存储过程或触发器中更新已经被调用的语句中使用的表。 这是因为 MySQL 禁止在触发器或存储过程中修改已经被调用的语句中使用的表。如果你需要在存储过程或触发器中更新表,那么你需要在存储过程或触发器中使用临时表来存储需要更新的数据,然后在存储过程或触发器的最后一步中将临时表中的数据更新到实际的表中。 以下是一个使用临时表的示例: ``` CREATE TEMPORARY TABLE temp_reserve ( r_id INT, p_id INT, d_id INT, r_time DATETIME, r_status VARCHAR(10), r_out_num INT ); INSERT INTO temp_reserve (r_id, p_id, d_id, r_time, r_status, r_out_num) VALUES (New.r_id, New.p_id, New.d_id, New.r_time, '已预约', @num - count); UPDATE reserve SET r_status = temp_reserve.r_status, r_out_num = temp_reserve.r_out_num FROM reserve INNER JOIN temp_reserve ON reserve.r_id = temp_reserve.r_id; DROP TABLE temp_reserve; ``` 在这个示例中,我们创建了一个临时表 temp_reserve,用于存储需要更新的数据。然后我们将需要更新的数据插入到临时表中。最后,我们使用 INNER JOIN 将临时表和实际的表 reserve 进行连接,并将临时表中的数据更新到实际的表中。更新完成后,我们删除临时表。 请注意,这只是一个示例,实际的存储过程或触发器可能需要根据具体情况进行修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值