示例如下:
复制代码 代码如下:
CREATE PROCEDURE `justifyGroupNum`()
NOT DETERMINISTIC
SQL SECURITY DEFINER
COMMENT ''
BEGIN
/*how to run:call justifyGroupNum()*/
DECLARE p_group_id int;
declare p_num int;
declare stopFlag int;
DECLARE cursor_name CURSOR
FOR select c_group_id,count(*) as num
from `t_group_member`
where c_valid in (3,4)
group by c_group_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND set stopFlag=1;
OPEN cursor_name;
REPEAT
FETCH cursor_name INTO p_group_id,p_num;
begin
update t_groupinfo set c_member_number=p_num where c_group_id=p_group_id;
end;
UNTIL stopFlag = 1
END REPEAT;
CLOSE cursor_name;
END;
总结:
1、注意设置游标的countinue handler:DECLARE CONTINUE HANDLER FOR NOT FOUND set stopFlag=1;
2、注意REPEAT和UTILE [停止条件] END REPEAT的使用,否则不会循环;
3、如何RUN,输入并执行:call justifyGroupNum()
复制代码 代码如下:
CREATE PROCEDURE `justifyGroupNum`()
NOT DETERMINISTIC
SQL SECURITY DEFINER
COMMENT ''
BEGIN
/*how to run:call justifyGroupNum()*/
DECLARE p_group_id int;
declare p_num int;
declare stopFlag int;
DECLARE cursor_name CURSOR
FOR select c_group_id,count(*) as num
from `t_group_member`
where c_valid in (3,4)
group by c_group_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND set stopFlag=1;
OPEN cursor_name;
REPEAT
FETCH cursor_name INTO p_group_id,p_num;
begin
update t_groupinfo set c_member_number=p_num where c_group_id=p_group_id;
end;
UNTIL stopFlag = 1
END REPEAT;
CLOSE cursor_name;
END;
总结:
1、注意设置游标的countinue handler:DECLARE CONTINUE HANDLER FOR NOT FOUND set stopFlag=1;
2、注意REPEAT和UTILE [停止条件] END REPEAT的使用,否则不会循环;
3、如何RUN,输入并执行:call justifyGroupNum()
关键字词:
相关文章
- mysql slave不能同步Last_SQL_Error Error Duplicate entry
- MySQL 提示Incorrect key file for table 'xx'; try to 出错无法打开
- MySQL Got error 28 from storage engine 解决办法
- 高可用的Mysql双机热备(Mysql_HA)
- mysql 对数据表的结构和数据进行复制
- mysql数据库表的错误 got error 28 from storage engine
- MySQL存储过程中使用动态行转列
- 查看运行MySQL中的语句(查询正在执行的sql)
- mysql安装图解 mysql图文安装教程(详细说明)
- Can't connect to MySQL server on localhost (10061)解决方法