我正在尝试在MySQL中创建我的第一个游标,但是我收到了一个错误。它表示不正确的整数值。我认为这会抓住customer_Id列中第一行的值,并将其存储到IdValue变量中。如何正确编码并修复此错误?
DELIMITER $$
CREATE PROCEDURE CursorProcedure()
BEGIN
DECLARE IdValue int;
DECLARE myCursor CURSOR FOR
SELECT customer_Id FROM customers;
OPEN myCursor;
FETCH myCursor INTO IdValue;
CLOSE myCursor;
SELECT IdValue;
END$$
DELIMITER ;
答案 0 :(得分:2)
DROP PROCEDURE IF EXISTS CursorProcedure;
DELIMITER $$
CREATE PROCEDURE CursorProcedure()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE IdValue int;
DECLARE myCursor CURSOR FOR SELECT customer_Id FROM customers;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN myCursor;
read_loop: LOOP
FETCH myCursor INTO IdValue;
IF done THEN
LEAVE read_loop;
END IF;
--
-- YOU ARE IN YOUR READ LOOP
-- DO SOMETHING WITH IT HERE
--
END LOOP;
CLOSE myCursor;
END$$
DELIMITER ;
但是在“在这里做点什么”中,不要对它进行SELECT,因为它会生成多个结果集。做一些有意义的事。
Cursors上的手册页。
顺便说一句,游标很少是你的朋友。它们非常慢。仅在紧急情况下使用它们。