MySQL游标不循环只返回第一条记录

时间:2017-12-01 05:55:17

标签: mysql stored-procedures

我正在尝试SP示例。此SP不循环。试图找出问题所在。任何帮助表示赞赏

DELIMITER $$

USE `DB`$$

DROP PROCEDURE IF EXISTS `build_email_list`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `build_email_list`()
BEGIN 
  DECLARE v_finished INTEGER DEFAULT 0;
  DECLARE v_email VARCHAR(100) DEFAULT "";  

  -- declare cursor for employee email
  DECLARE email_cursor CURSOR FOR SELECT `email` FROM `people`;

  -- declare NOT FOUND handler
  DECLARE CONTINUE HANDLER 
    FOR NOT FOUND SET v_finished = 1;

  OPEN email_cursor;

  get_email: LOOP    
    FETCH email_cursor 
    INTO  v_email;   

    IF v_finished = 1 THEN 
      LEAVE get_email;
    END IF;

    -- print list
    SELECT v_email;  
  END LOOP get_email;

  CLOSE email_cursor;    
END$$

DELIMITER ;

0 个答案:

没有答案