I’m trying to use a cursor loop in MYSQL, but it’s not working. I’ve essentially copied the example from http://dev.mysql.com/doc/refman/5.0/en/cursors.html, except I use a function instead of a procedure. Does that matter?
When I try to run the function – select xxx() from dual – I get an error: unknown column “done” in ‘field list’. What to do?
delimiter $$
create function xxx()
returns int deterministic
begin
DECLARE a INT;
DECLARE cur1 CURSOR FOR SELECT id FROM my_table;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE cur1;
return 1;
end$$
delimiter ;
You didnt declared
done.DECLARE done INT DEFAULT FALSE;and at the end
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;