How to check if MySQL cursor is empty

You can add a continue handler to your cursor.
declare v_emailid bigint(20);
DECLARE done INT DEFAULT FALSE;
DECLARE cursor_email CURSOR FOR select e.emailid from email e WHERE
e.status='pending';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cursor_email;
read_loop: LOOP
FETCH cursor_email INTO v_emailid;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE email e
SET
e.status = 'new'
WHERE
e.emailid = v_emailid;
END LOOP;
close cursor_email;

Comments

Popular posts from this blog

Check MySQL query history from command line

Installing chocolatey on Windows