Example:
CopyCode The Code is as follows: 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;
Summary:
1. Pay attention to the countinue handler: declare continue handler for not found set stopflag = 1;
2. Pay attention to the use of repeat and utile [Stop Condition] End repeat, otherwise it will not be cyclically;
3. Run the following command: Call justifygroupnum ()