在存储过程中打印多行

时间:2013-04-20 13:36:07

标签: mysql stored-procedures phpmyadmin

我想传入一个int值并返回一个小于此int的短密码列表。我在修改MySQL游标示例时遇到了问题:http://dev.mysql.com/doc/refman/5.0/en/cursors.html。我想用短密码打印出行,但是只要结果有多行,PhpMyAdmin就会无限期地显示“处理请求”。我究竟做错了什么?它破坏了我周六的生产力!

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ShortPasswd`(IN `passwordLength` TINYINT)
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE a INT;
  DECLARE b VARCHAR(255);
  DECLARE cur1 CURSOR FOR SELECT UID, Password
      FROM authentication WHERE CHARACTER_length(Password) < passwordLength;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

  OPEN cur1;

  read_loop: LOOP
    FETCH cur1 INTO a, b;
    IF done THEN
      LEAVE read_loop;
    END IF;
    SELECT b;
  END LOOP;

  CLOSE cur1;
END
$$
DELIMITER ;

2 个答案:

答案 0 :(得分:2)

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ShortPasswd`(IN `passwordLength` TINYINT)
BEGIN

   SELECT UID, Password
      FROM authentication WHERE CHARACTER_length(Password) > passwordLength;

END
$$
DELIMITER ;

您将直接获得行UID,密码。当你

时从查询中获得
call ShortPasswd;

答案 1 :(得分:0)

我建议修改这些行:

...
DECLARE FINISHED BOOLEAN DEFAULT 0;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

...
IF done THEN
  CLOSE cur1;
  LEAVE read_loop;
END IF;

...
END LOOP read_loop;

我使用0/1代替FALSE / TRUE,并在离开LOOP之前关闭光标。