处理存储过程中的select语句返回的行

时间:2012-10-19 02:42:48

标签: mysql stored-procedures

是否有人可以告诉我如何在stored procedure内获取行?

比方说我做

BEGIN 
   SELECT id, name, lastname, 
   FROM eyploees;

   --How can i process all the results within this procedure?

END<br/>

1 个答案:

答案 0 :(得分:3)

您需要使用游标:

DECLARE id_var INT;
DECLARE name_var VARCHAR(255);
DECLARE lastname_var VARCHAR(255);

DECLARE my_cursor CURSOR FOR SELECT id, name, lastname FROM employees;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN my_cursor;

read_loop: LOOP

    FETCH my_cursor INTO id_var, name_var, lastname_var;
    IF done THEN
      LEAVE read_loop;
    END IF;

    --Do some row work here using _var variables from above

END LOOP;

CLOSE my_cursor;