MySQL · 游标

 1 BEGIN
 2  
 3 --定义变量
 4 declare testrangeid BIGINT;
 5 declare versionid BIGINT; 
 6 declare done int;
 7 --创建游标,并存储数据
 8 declare cur_test CURSOR for 
 9    select id as testrangeid,version_id as versionid from tp_testrange;
10 --游标中的内容执行完后将done设置为1
11  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; 
12 --打开游标
13 open cur_test;
14 --执行循环
15   posLoop:LOOP
16 --判断是否结束循环
17         IF done=1 THEN  
18       LEAVE posLoop;
19     END IF; 
20 --取游标中的值
21     FETCH  cur_test into testrangeid,versionid;
22 --执行更新操作
23     update tp_data_execute set version_id=versionid where testrange_id = testrangeid;
24   END LOOP posLoop;
25 --释放游标
26 CLOSE cur_test;
27  
28 END
原文地址:https://www.cnblogs.com/wsyjlly/p/9648046.html