我甚至无法在我的TRANSACTION中运行这个基本的CURSOR LOOP。
请帮忙。提前谢谢!
CREATE PROCEDURE ClearItems(IN itemName CHAR)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE bID INT(255);
DECLARE bIDs CURSOR FOR SELECT id FROM table1 WHERE column1 = itemName AND column2 = 'b' ORDER BY column3 DESC, date ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
START TRANSACTION;
OPEN bIDs;
clear_loop: LOOP
FETCH bIDs INTO bID;
IF done THEN
LEAVE clear_loop;
END IF;
INSERT INTO table2 (column1) SELECT bID;
END LOOP;
CLOSE bIDs;
COMMIT;
END //
DELIMITER ;
答案 0 :(得分:2)
解决问题 - 重命名声明的ID变量。字段名称和变量不应该相同。
另外,如果要填充另一个表,请尝试使用INSERT ... SELECT语句。它将帮助您避免使用游标。例如 -
INSERT INTO table2(`2column1`)
SELECT id
FROM table1
WHERE `1column1` = itemName AND `1column2` = 'b'
ORDER BY `1column3` DESC, date ASC;