我有一个存储过程,它更新两个表,然后将一行插入第三个表。 (根据我的理解,回滚更新或插入,如果所有三个都无法完成)无论如何,无论何时我运行此程序,无论它是否最终成功运行两个更新并插入或不插入,它将始终“成功”并且返回false。我需要它返回一个值(可能是真的),如果它成功完成所有两个更新和一个插入,如果没有则返回false,这样我就可以在服务器端进行正确的错误检查。
以下是程序:
CREATE DEFINER=`db1`@`%` PROCEDURE `completed_procedure`(IN insubmissionid INT)
BEGIN
DECLARE ingregid INT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
START TRANSACTION;
SET @ingregid = (SELECT idgreg FROM submission WHERE idsubmission = insubmissionid);
UPDATE submission SET status='a' WHERE idsubmission = insubmissionid;
UPDATE greg SET iscomplete=1 WHERE idgreg = @ingregid;
INSERT INTO completion (idperson, idgreg, datecompleted, idsubmission, mediaid, description,
privatestatus, contenttype, totalamount)
SELECT s.idperson, s.idgreg, s.datesubmitted, s.idsubmission,
COALESCE(s.youtubeid, s.contentid) AS mediaidid, g.description, g.privatestatus,
g.contenttype, COALESCE(SUM(amount),0) AS totalamount
FROM submission s
INNER JOIN greg g on s.idgreg = g.idgreg
INNER JOIN contribution c on s.idgreg = c.idgreg
WHERE s.idsubmission = insubmissionid AND c.ispaid = 1;
COMMIT;
END
最好的方法是什么?
答案 0 :(得分:0)
你可以返回一个值,这是一个例子。
CREATE DEFINER=`db1`@`%` PROCEDURE `completed_procedure`(IN insubmissionid INT, OUT success INT
BEGIN
IF ( CONDITION ) THEN
SET success = 1;
ELSE
SET success = -1;
END IF;
SELECT success ;
COMMIT;
END