0

我的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

remainingUses如您所见,仅当初始select语句中的变量大于“0”时才应执行更新语句。

但是,当我调用该过程时CALL UsePassword(197);,它会返回Affected rows: 1.

我不明白,当我id = 197在数据库中的密码行的值为“remaining_uses = 0”时。

有没有理由为什么 if 会显示Affected rows: 1在结果中?

如果该语句成功执行,它是否会返回 1 个受影响的行?因为从技术上讲,在这个例子中我的 UPDATE 语句没有被执行。

更新不仅没有更新,而且如果我完全删除更新语句,它仍然告诉我有一个受影响的行!

谢谢

4

1 回答 1

2

最后,我通过在我的存储过程中声明一个 OUT 变量然后通过在SELECT语句之后设置它的值来返回值来解决问题。

/* Return the affected rows */
SET affected_rows = ROW_COUNT();
于 2013-03-30T08:20:44.823 回答