我无法执行我的存储过程,我的查询只是继续运行

时间:2014-06-12 09:52:39

标签: mysql stored-procedures

我只是尝试在mysql中使用存储过程。我试图创建一个表名“Book”。我以前有5列。它们是idBook,bookName,bookYear和Stock。所以,我添加了2列用于评级和审核。我想在存储过程中使用循环给这些列赋值。这是我的代码:

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `getBookExcitement`()
BEGIN

DECLARE bookRating INT DEFAULT 0;
DECLARE count INT DEFAULT 0;
DECLARE bookReview VARCHAR (100);

DECLARE sums INT;
SELECT count(idBook) INTO sums FROM Book;



getBookExcitement : LOOP

SET count = count+1;

    IF count<sums then

        SELECT FLOOR(1 + (rand()*5)) INTO bookRating;

            IF bookRating <3 then

                SET bookReview = 'This book isn't good';


            ELSEIF bookRating <=4 OR bookRating  >=3 then

                SET bookReview = 'This book is good';

            ELSE 

                SET bookReview = 'This book is very good, you should read it';

            END IF;

                UPDATE Book SET Rating = bookRating AND Review = bookReview 
                WHERE idBuku=count;
    END IF;


END LOOP getBookExcitement;

END

但是当我尝试调用它时,我的工作台只是不停地执行查询。就像它陷入循环一样。但是我看到它的方式,我无法找到循环的错误,请帮助我,因为我是新的存储过程在mysql。

感谢

1 个答案:

答案 0 :(得分:0)

没有LEAVE getBookExcitement;else放在IF count<sums then块的末尾,并带有LEAVE语句。