du skal definere en fortsæt-handler:
DROP PROCEDURE IF EXISTS Test;
DELIMITER //
CREATE PROCEDURE Test()
BEGIN
-- Our columns
DECLARE name VARCHAR(45);
DECLARE id INT DEFAULT -1;
DECLARE done INT DEFAULT 0;
-- Our cursor
DECLARE cur CURSOR FOR SELECT name, id FROM MyTable WHERE id > 3;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
DROP TEMPORARY TABLE IF EXISTS TempTest;
CREATE TEMPORARY TABLE TempTest
(
name VARCHAR(45) NOT NULL,
id INT NOT NULL
);
-- Open our cursor
open cur;
-- Start our for loop
forLoop: LOOP
-- Get the row
FETCH cur INTO name, id;
IF done = 1 THEN
LEAVE forLoop;
END IF;
INSERT INTO TempTest (name, id)
VALUES ( name, id);
END LOOP forLoop;
-- Close the cursor
CLOSE cur;
-- NOW GET THE RESULTS
SELECT * FROM TempTest;
END; //
DELIMITER ;