Created
June 24, 2014 08:10
-
-
Save beginor/b807e3571ceab0d90bad to your computer and use it in GitHub Desktop.
MySql Cursor Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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