wuyulin0903
wuyulin0903
2016-08-19 08:58

关于mysql存储过程游标只循环一次

DELIMITER $$

CREATE PROCEDURE comment_count_sp ()
BEGIN
-- 需要定义接收游标数据的变量
DECLARE news_id_bak INT(11);
DECLARE news_count INT(11);
DECLARE count_all INT(11) DEFAULT 0;
-- 遍历数据结束标志
DECLARE done INT DEFAULT FALSE;
-- 游标
DECLARE cur CURSOR FOR (
SELECT news_info_id news_id_bak,COUNT(news_info_id) news_count FROM t_news_comment WHERE STATUS = 1 GROUP BY news_id_bak ORDER BY news_count ASC );

-- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
SET done = FALSE;
-- 打开游标
OPEN cur;

-- 开始循环
read_loop: LOOP

FETCH cur INTO news_id_bak,news_count;

IF done THEN
  LEAVE read_loop;
END IF;

SET count_all = count_all + 1;

END LOOP;
SELECT count_all;
-- 关闭游标
CLOSE cur;

END

这个存储过程为什么count_all总是1,快崩溃了

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐