MySQL 存储过程和"Cursor"的使用方法
示例如下:
总结:
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()
相关推荐
Justdoit00 2020-04-26
dreamhua 2020-01-31
nicepainkiller 2020-01-06
JackLang 2019-12-19
zhangchaoming 2019-12-18
pythonxuexi 2019-12-16
暗夜之城 2019-11-13
bigdatazx 2010-11-05
qsdnet我想学编程 2019-11-04
DAV数据库 2019-10-26
圆圆的世界CSDN 2019-10-22
nickey 2011-11-17
ebuild 2017-02-14
zhaoshuliok 2012-09-12
人勤阡陌绿 2019-03-27
nosqlnotes 2014-08-16