这是我正在尝试做的事情:
当INSERT表格中有新内容时ACCOUNTS,我需要更新ACCOUNTSwhere pk=中的行,NEW.edit_on通过设置status='E'来表示特定(旧)帐户已被编辑.
DELIMITER $$
DROP TRIGGER IF EXISTS `setEditStatus`$$
CREATE TRIGGER `setEditStatus` AFTER INSERT on ACCOUNTS
FOR EACH ROW BEGIN
update ACCOUNTS set status='E' where ACCOUNTS.pk = NEW.edit_on ;
END$$
DELIMITER ;
Run Code Online (Sandbox Code Playgroud)
要求不是我操纵新插入的列,而是操作已经存在的列pk = NEW.edit_on
但是,我无法更新同一个表: Can't update table ACCOUNTS ... already used by the statement that invoked this trigger
请建议一个解决方法
PS:我已经通过已经走在触发更新表上相同的表更新后,插入到相同的表触发的MySQL,在同一个表插入触发器后更新和对表后插入MySQL的触发器与插入和更新,但他们不似乎回答我的问题.
编辑 …
我正在尝试修改我的MySQL存储过程并使其成为事务性的.现有的存储过程工作正常,没有问题,但只要我进行事务处理,它甚至不允许我保存我的更改.我检查了MySQL文档并在线搜索,但我发现我的代码没有任何问题.这似乎很直接,但无法弄清楚.
BEGIN
DECLARE poid INT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION SQLWARNING
BEGIN
ROLLBACK;
END
START TRANSACTION;
-- ADD option 5
INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,5,0);
SET poid = (SELECT LAST_INSERT_ID());
INSERT INTO product_option_value(product_option_id,product_id,option_id,option_value_id,quantity,subtract,price,price_prefix,points,points_prefix,weight,weight_prefix) VALUES(poid,insertedProductID,5,50,0,0,4.99,'+',0,'+',0,'+');
-- ADD option 12
INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,12,1);
-- ADD option 13
INSERT INTO product_option(product_id,option_id,required) VALUES(insertedProductID,13,0);
COMMIT;
END
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
如果SQL语句中有任何错误,系统将自动回滚更改,如何启动事务?
用PHP
try {
// First of all, let's begin a transaction
$db->beginTransaction();
// A set of queries; if one fails, an exception should be thrown
$db->query('first query');
$db->query('second query');
$db->query('third query');
// If we arrive here, it means that no exception was thrown
// i.e. no query has failed, and we can commit the transaction
$db->commit();
} catch (Exception $e) {
// An exception has been thrown
// We must rollback the transaction
$db->rollback();
} …Run Code Online (Sandbox Code Playgroud) 正如标题所说,MySQL 中的存储过程是原子的吗?即会像
for (..)
<check_if_row_has_flag>
for (..)
<update_row>
Run Code Online (Sandbox Code Playgroud)
原子地工作?
有趣的是,除了 2009 年的一个论坛帖子外,我在 Google 上找不到太多关于此的信息。