1.定义游标
DECLARE cursor_name CURSOR FOR select_statement
2.打开游标
OPEN cursor_name
3.遍历游标
FETCH cursor_name INTO var_name [, var_name] ...
4.关闭游标
CLOSE cursor_name
5.清除游标
declear cursor_name
示例:
BEGIN
DECLARE no_more_record INT DEFAULT 0;
DECLARE pID BIGINT(20);
DECLARE pValue DECIMAL(15,5);
DECLARE cur_record CURSOR FOR SELECT colA, colB from tableABC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_record = 1;
OPEN cur_record;
FETCH cur_record INTO pID, pValue;
WHILE no_more_record != 1 DO
INSERT INTO testTable(ID, Value)
VALUES (pID, pValue);
FETCH cur_record INTO pID, pValue;
END WHILE;
CLOSE cur_record;
END