我的 mysql
数据库中有以下存储过程:
BEGIN
DECLARE useCount, remainingUses INT DEFAULT 0;
/* Get the current values for the quiz into the variables */
SELECT remaining_uses, use_count INTO remainingUses, useCount FROM quiz_passwords WHERE password_id = passwordId;
/* Are there remaining uses to consume? */
if (remainingUses > 0) THEN
UPDATE quiz_passwords SET use_count = (useCount + 1), remaining_uses = (remainingUses - 1) where password_id = passwordId;
END IF;
END
如您所见,只有当初始 select
语句中的 remainingUses
变量大于“0”时,才应执行更新语句。
但是,当我调用过程CALL UsePassword(197);
时,它返回Affected rows: 1
。
我不明白,数据库中 id = 197
的密码行的值为“remaining_uses = 0”。
如果会在结果中显示Affected rows: 1
,是否有原因?
如果该语句执行成功,是否会返回 1 个受影响的行?因为从技术上讲,在这个示例中我的 UPDATE 语句没有被执行。
更新不仅没有任何更新,而且如果我完全删除更新语句,它仍然告诉我有一个受影响的行!
谢谢
最佳答案
最后,我通过在存储过程中声明一个 OUT 变量,然后通过在 SELECT
语句之后设置它的值来返回该值,解决了问题。
/* Return the affected rows */
SET affected_rows = ROW_COUNT();
关于MySQL 过程在未更新任何内容时返回 1 个受影响的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15708020/