- CREATE DEFINER=`root`@`%` PROCEDURE `init_reportUrl`()
- BEGIN
- -- 定义变量
- DECLARE s INT DEFAULT 0;
- DECLARE cid bigint(11);
- DECLARE aa VARCHAR(11);
-
- -- 定义游标,并将sql结果集赋值到游标中
- DECLARE report CURSOR FOR SELECT id,a FROM ab;
- -- 声明当游标遍历完后将标志变量置成某个值
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET s=1;
- -- 打开游标
- OPEN report;
- FETCH report INTO cid,aa;
- -- 当s不等于1,也就是未遍历完时,会一直循环
- WHILE s<>1 DO
- -- 执行业务逻辑
- -- code放在这里表示没遍历一次重新生成一次
-
- UPDATE ab2 set a=(SELECT a from ab where id=cid) where id=cid ;
-
- -- 将游标中的值再赋值给变量,供下次循环使用
- FETCH report INTO cid,aa;
- -- 当s等于1时表明遍历以完成,退出循环
- END WHILE;
- -- 关闭游标
- CLOSE report;
- END
- DROP PROCEDURE test_insert;
-
- CREATE PROCEDURE test_insert () BEGIN
- DECLARE i INT;
-
- SET i = 1;
-
- WHILE
- i < 51 DO
- INSERT INTO `ods_common`.`test`(`id`) VALUES (round(rand() * 1000, 0));
-
- SET i = i + 1;
-
- END WHILE;
-
- END;