存储过程:
create PROCEDURE updateCode()
BEGIN
DECLARE i int;
DECLARE s int DEFAULT 0;
DECLARE pid VARCHAR(50);
-- 定义游标,并将sql结果集赋值到游标中
DECLARE report CURSOR FOR select id from compute;
-- 声明当游标遍历完后将标志变量置成某个值
DECLARE CONTINUE HANDLER FOR NOT FOUND SET s=10;
--打开游标
open report;
--将游标赋值给变量
fetch report into pid;
set i = 1;
--循环跳出条件
WHILE s<>10 DO
--循环体语句
UPDATE compute set ruleCode=LPAD(i, 8, 0) where id = pid;
set i=i+1;
--将游标中的值再赋值给变量
fetch report into pid;
END WHILE;
--关闭游标
close report;
END
--执行存储过程
CALL updateCode();