DELIMITER $$
CREATE PROCEDURE processEmployees()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE nameVal VARCHAR(50);
DECLARE phoneNumberVal VARCHAR(50);
DECLARE idVal INT;
DECLARE cur1 CURSOR FOR SELECT id,firstName FROM employee;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- opening the cursor
OPEN cur1;
loopEmp: LOOP
FETCH cur1 INTO idVal ,nameVal ; -- fetching the values into the local variables
-- If the value of done is set to true then leave the loop
IF done THEN
LEAVE loopEmp;
END IF;
INSERT INTO logTable SELECT CONCAT('For id=',idVal ,' , value of done before inner select = ',done) ;
SELECT PhoneNumber INTO phoneNumberVal FROM employeecommunicationinfo WHERE employeeId = idVal ;
INSERT INTO logTable SELECT CONCAT('For id=',idVal ,' , value of done after inner select = ',done) ;
END LOOP;
CLOSE cur1;
END$$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE processEmployees()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE actualDone INT DEFAULT FALSE;
DECLARE nameVal VARCHAR(50);
DECLARE phoneNumberVal VARCHAR(50);
DECLARE idVal INT;
DECLARE cur1 CURSOR FOR SELECT id,firstName FROM employee;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- opening the cursor
OPEN cur1;
loopEmp: LOOP
FETCH cur1 INTO idVal ,nameVal ; -- fetching the values into the local variables
-- If the value of done is set to true then leave the loop
IF done THEN
LEAVE loopEmp;
END IF;
INSERT INTO logTable SELECT CONCAT('For id=',idVal ,' , value of done before inner select = ',done) ;
-- Assigning the original value of done to a temp variable
set actualDone = done;
SELECT PhoneNumber INTO phoneNumberVal FROM employeecommunicationinfo WHERE employeeId = idVal ;
-- Assigning back the value of done
set done = actualDone;
INSERT INTO logTable SELECT CONCAT('For id=',idVal ,' , value of done after inner select = ',done) ;
END LOOP;
CLOSE cur1;
END$$
DELIMITER ;
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)