MySQL Stored Procedure cursor loop operation

Source: Internet
Author: User
Code

Label_proc:
Begin

-- Key value used for task table Association
Declare v_faxkey varchar (50) default '';

-- Fax status in the task table
Declare v_taskstate int default 0;
Declare v_id int default 0;
Declare v_username varchar (50) default '';

Declare done int default 0;
Declare cur1 cursor for select ID from childfax where parentid = p_id;
Declare continue handler for sqlstate '000000' set done = 1;

-- If the ID is not in the valid range,-1 is returned.
If (p_id <= 0) then
Select-1 into result;
Leave label_proc;
End if;

-- ID: the specified record does not exist.-2 is returned directly.
If (not exists (select * From parentfax where id = p_id) then
Select-2 into result;
Leave label_proc;
End if;

If (select count (*) from faxtask where parentid = p_id and status = 1> 0) then
Select-3 into result;
Select 'sending in progress, cannot delete ';
Leave label_proc;
End if;

-- Setting the sub-fax will delete the status
Update childfax set faxstate =-1 where parentid = p_id;

-- Set the fax to the deleted status.
Update parentfax set faxstate =-1 where id = p_id;

-- Delete data in the task table
Delete from faxtask where parentid = p_id;

Open cur1;

Repeat
Fetch cur1 into v_id;

If not done then
Select username into v_username from user_childfax_view where id = v_id;
Call pro_add_log (v_id, 2, Concat (v_username, 'delete '));
End if;

Until done end repeat;
Close cur1;

-- Return successful execution results
Select 1 into result;

End label_proc

 

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.