| 導購 | 订阅 | 在线投稿
分享
 
 
 

MySQL的存儲過程寫法和"Cursor"的使用

來源:互聯網  2008-06-01 02:14:22  評論

本文中介紹了一個MySQL的存儲過程,其中涉及Cursor的使用,示例如下:

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的存儲過程,其中涉及Cursor的使用,示例如下: 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()
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有