MySQL 存储过程和"Cursor"的使用方法
程序员文章站
2023-11-12 13:59:04
示例如下: 复制代码 代码如下:create procedure `justifygroupnum`() not deterministic sql security de...
示例如下:
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的校对规则引起的问题分析
下一篇: CAD编辑器图纸怎么插入程序包?