我正在尝试在 MySQL 存储过程中使用事务。
具体来说,使用临时记录中的修改数据更新用户表。从另一张 table 。 然后一旦转移,删除临时记录。
我创建了下面的代码,执行时返回字符串“交易已成功”。
但是,实际上没有更新任何内容,并且临时记录也没有被删除。
两个 SQL 语句在单独执行时都可以正常工作,第一个执行更新,第二个执行删除。
谁能告诉我可能出了什么问题吗?
BEGIN
-- set a default response
DECLARE response varchar(48) DEFAULT "the transaction has failed.";
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
-- set vars
SET response = "the transaction has failed, you may have already updated the account.";
select response;
END;
START TRANSACTION;
-- we are inserting data, using information from another table
update user, updateUserNamesAndNumbers
SET user.firstName = updateUserNamesAndNumbers.firstName,
user.lastName = updateUserNamesAndNumbers.lastName,
user.landline = updateUserNamesAndNumbers.landline,
user.mobile = updateUserNamesAndNumbers.mobile
WHERE
updateUserNamesAndNumbers.uuid = transferCode
AND
updateUserNamesAndNumbers.userId= user.user_id
;
-- finally delete the original tuple
DELETE from updateUserNamesAndNumbers
where uuid= transferCode ;
SET response="The transaction has succeeded";
COMMIT;
选择响应;
结束
最佳答案
将隐式连接更改为显式连接
update user join updateUserNamesAndNumbers on updateUserNamesAndNumbers.uuid = transferCode
关于mysql - MySql 存储过程中的事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52646713/