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