DELIMITER $$
USE `student`$$
DROP PROCEDURE IF EXISTS `sync_student`$$
CREATE DEFINER=`student`@`%` PROCEDURE `sync_student`(para_name varchar) ----记住varchar类型的参数一定要设置长度,修改为varchar(50),就OK了
BEGIN
-- 定义变量
DECLARE id INT;
DECLARE name varchar;
DECLARE course INT;
DECLARE done INT DEFAULT 0;
-- 定义一个游标
DECLARE curCursor CURSOR FOR
SELECT
d.name AS name,
d.course as course
FROM
student d
WHERE 1 = 1
AND d.name = para_name ;
-- 如果游标指向没有记录,则定义done=1
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
-- 开始事务
START TRANSACTION;
-- 打开游标
OPEN curCursor;
-- 循环开始
stock_loop:LOOP
-- 获取游标的值,赋值给变量
FETCH curCursor INTO name;
-- 如果游标已经执行完,则跳出stock_loop 循环
IF done=1 THEN
COMMIT;
LEAVE stock_loop;
END IF;
......
-- 循环结束
END LOOP stock_loop;
-- 关闭当前游标
CLOSE curCursor;
-- 事务提交
COMMIT;
END$$
DELIMITER ;