REPEAT比较好用

BEGIN
	#Routine body goes here...
DECLARE iID_ALL int(11);
DECLARE iIN_COUNT_SUM int(11);
DECLARE sVR_WWE_REMARK varchar(255);
DECLARE sVR_WWF_REMARK varchar(255);

DECLARE sDone int(1);
DECLARE iCount int(11);

--create 游标
DECLARE cTEST_cur CURSOR FOR
SELECT ID_ALL,IN_COUNT_SUM,VR_WWE_REMARK,VR_WWF_REMARK  FROM tb_all;

--Definition 条件
DECLARE EXIT HANDLER FOR NOT FOUND BEGIN END;
--DECLARE CONTINUE HANDLER FOR sqlstate '02000' SET sDone=1;

--REPEAT 需要的起始和结束条件
SET iCount=(SELECT COUNT(*) FROM tb_all);
SET sDone=0;

--打开游标和开始REPEAT
OPEN cTEST_cur;
REPEAT 

--将游标里的值赋值 和 其他相关操作动作。
FETCH cTEST_cur INTO iID_ALL,iIN_COUNT_SUM,sVR_WWE_REMARK,sVR_WWF_REMARK;
INSERT tb_all_bak VALUES(iID_ALL,iIN_COUNT_SUM,sVR_WWE_REMARK,sVR_WWF_REMARK);
--结束变量的增加
SET sDone = sDone + 1;

--当条件满足后 跳出REPEAT & 关闭游标
UNTIL sDone>=iCount END REPEAT;
CLOSE cTEST_cur;

END

你可能感兴趣的:(sql)