我有一个mysql存储过程,如果它能够成功执行两个更新并且插入并回滚所有内容,则返回1,如果不能,则返回0。这是它的样子:
CREATE DEFINER=`barrence`@`%` PROCEDURE `completed_procedure`(IN insubmissionid INT, OUT success INT)
BEGIN
DECLARE inbountyid INT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
DECLARE EXIT HANDLER FOR SQLWARNING ROLLBACK;
START TRANSACTION;
SET success = 0;
SET @inbountyid = (SELECT idbounty FROM submission WHERE idsubmission = insubmissionid);
UPDATE submission SET status='a' WHERE idsubmission = insubmissionid;
IF (SELECT ROW_COUNT() = 1)
THEN UPDATE bounty SET iscomplete=1 WHERE idbounty = @inbountyid;
END IF;
IF (SELECT ROW_COUNT() = 1)
THEN INSERT INTO completion (idperson, idbounty, datecompleted, idsubmission, mediaid, description,
privatestatus, contenttype, totalamount)
SELECT s.idperson, s.idbounty, s.datesubmitted, s.idsubmission,
COALESCE(s.youtubeid, s.contentid) AS mediaidid, b.description, b.privatestatus,
b.contenttype, COALESCE(SUM(amount),0) AS totalamount
FROM submission s
INNER JOIN bounty b on s.idbounty = b.idbounty
INNER JOIN contribution c on s.idbounty = c.idbounty
WHERE s.idsubmission = insubmissionid AND c.ispaid = 1;
END IF;
IF (SELECT ROW_COUNT() = 1)
THEN SET success = 1;
END IF;
IF (success = 0)
THEN ROLLBACK;
END IF;
SELECT success;
COMMIT;
END
当我在MySQL Workbench中调用此存储过程时,它似乎正常工作,执行所有更新/插入并返回1或0否则。但是,当我在我的node.js服务器和console.log上调用此存储过程时,它只返回成功变量,如果它是1但是如果为0则不返回它。这是我如何调用它:
db.acceptSubmission(submissionId, function(result) {
console.log(result);
}
exports.acceptSubmission = function(submissionId, callback) {
var sql = "CALL completed_procedure(?, @success)";
var inserts = [submissionId];
sql = mysql.format(sql, inserts);
console.log("Running Admin Accept Submission Procedure: " + sql);
dbcommon.executeStatement(pool, sql, callback);
};
成功时结果如下:
[ [ { success: 1 } ],
{ fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0 } ]
这不是:
{ fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0 }
我完全被难过了。发生了什么事?
答案 0 :(得分:0)
我相信这就是你在使用的mysql模块在该上下文中看到结果时的行为方式。我相信,既然您知道如何解释响应,那么在这种情况下这是一个非问题,并且当您返回非零响应时,您将得到预期的结果。