-- CREATION DU CURSEUR

DELIMITER $$
CREATE PROCEDURE createEmailListN (
	INOUT emailList VARCHAR(4000)
)
BEGIN
	DECLARE finished INTEGER DEFAULT 0;
	DECLARE emailAddress VARCHAR(100) DEFAULT "";

	-- declare cursor for employee email
	DECLARE curEmail 
		CURSOR FOR 
			SELECT emailBeneficiaire FROM beneficiaire WHERE emailBeneficiaire !='';

	-- declare NOT FOUND handler
	DECLARE CONTINUE HANDLER 
        FOR NOT FOUND SET finished = 1;

	OPEN curEmail;

	getEmail: LOOP
		FETCH curEmail INTO emailAddress;
		IF finished = 1 THEN 
			LEAVE getEmail;
		END IF;
		-- build email list
		SET emailList = CONCAT(emailAddress,";",emailList);
	END LOOP getEmail;
	CLOSE curEmail;

END$$
DELIMITER ;



-- EXECUTION DU CURSEURS

SET @emailList = ""; 

CALL createEmailListN(@emailList); 

SELECT @emailList;