MySQL stored procedures and "Cursor" methods of Use _mysql

Source: Internet
Author: User
Examples are as follows:
Copy Code code 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;

Summarize:
1. Note Set cursor countinue Handler:declare CONTINUE handler for not FOUND set stopflag=1;
2, pay attention to repeat and utile [stop condition] end repeat use, otherwise will not cycle;
3. How to run, enter and execute: Call Justifygroupnum ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.