如何编写MySQL存储过程和使用“当前”
2008-03-11
eNetCiweek
本文介绍了与使用Cursor相关的MySQL存储过程。 例如以下所示。
创建进程` justifygroupnum ` (
非细节
SQL SECURITY DEFINER
COMMENT ' '
比根
/* how torun : calljustifygroupnum () ) /
DECLARE p_group_id int;
declare p_num int;
取消停止标志int;
DECLARE cursor_name CURSOR
FOR select c_group_id,count(* ) as num
from `t_group_member `
wherec _ validin (3,4 ) ) ) ) ) ) ) ) wherec _ validin (3,4 ) ) ) ) ) wherec _ validin (3,4 ) ) ) ) ) ) ) ) wher ) ) ) wher。
group by c_group_id;
declarecontinuehandlerfornotfoundsetstopflag=1;
OPEN cursor_name;
REPEAT
FETCH cursor_name INTO p_group_id,p_num;
比根
updatet _ group infos etc _ member _ number=p _ numwherec _ group _ id=p _ group _ id;
结束;
UNTIL stopFlag=1
结束报告;
CLOSE cursor_name;
结束;
总结:
1、注意光标countinuehandler的设置: declarecontinuehandlerfornotfoundsetstopflag=1;
2、请注意使用REPEAT和UTILE [停止条件] END REPEAT。 不这样做的话就不会循环;
3、如何运行,输入运行: call justifyGroupNum () ) )。
如果对这篇文章或本站有意见,请在下面提出。 谢谢你。