Skip to content

Instantly share code, notes, and snippets.

@beginor
Created June 24, 2014 08:10
Show Gist options
  • Select an option

  • Save beginor/b807e3571ceab0d90bad to your computer and use it in GitHub Desktop.

Select an option

Save beginor/b807e3571ceab0d90bad to your computer and use it in GitHub Desktop.
MySql Cursor Demo
DELIMITER $$
CREATE DEFINER=`udev`@`%` PROCEDURE `build_email_list`(INOUT email_list varchar(4000))
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 employees;
-- 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;
-- build email list
SET email_list = CONCAT(v_email,";",email_list);
END LOOP get_email;
CLOSE email_cursor;
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment