CREATE PROCEDURE test
BEGIN
-- 定义参数
DECLARE _id INT;
-- 定义游标
DECLARE no_more_products INT DEFAULT 0;
DECLARE RESULT_test CURSOR FOR
SELECT num1 from numtest;
-- 如果NOT FOUND游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_products = 1;
-- 打开游标
OPEN RESULT_test;
-- 开始循环
REPEAT
-- 读取每一行
FETCH RESULT_test INTO _id;
-- 插入语句
insert into B(id) VALUES(_id);
-- 如果参数等于1跳出
UNTIL no_more_products = 1
--结束循环
END REPEAT;
--关闭游标
CLOSE RESULT_test;
END
调用存储过程: