1. 程式人生 > >Mysql儲存過程遞迴

Mysql儲存過程遞迴

公司專案的業務系統中,沒有使用公司的工作流,使用標誌位來玩。所有就牽涉到刪除代辦。不說了,直接看程式碼。

1、在刪除的時候我想使用mysql的split函式分割,後來發現沒有這個函式,只好自己寫一個了。

/**********返回數量***************/
create function getSplitCount(splitString varchar(4000),splitChar varchar(20))
returns int
begin
 return (1+(length(splitString) - length(replace(splitString,splitChar,''))));
end;

/**********獲取值******************/
create function getSplitValue(splitString varchar(4000),splitChar varchar(20),saplit_count int)
returns varchar(400)
begin
 return reverse(substring_index(reverse(substring_index(splitString,splitChar,saplit_count)),splitChar,1));
end;
2、刪除代辦

/********* 刪除代辦的儲存過程 **********/
drop procedure IF EXISTS delete_process_task_proc;
create procedure delete_process_task_proc (in parm_taskid varchar(32))
begin
  declare parm_task_id varchar(4000);
     set parm_task_id = parm_taskid;
  call delete_process_task_proc(parm_task_id);
        call delete_down_process_task_proc(parm_task_id);
end;

/******* 刪除上級代辦代辦的儲存過程 *******/
drop procedure IF EXISTS delete_up_process_task_proc;
create procedure delete_up_process_task_proc (parm_taskid varchar(32))
begin
   declare parm_up_taskid varchar(4000);
   declare parm_branch_count int(11);
  /**** 查詢上級代辦****/
      select up_taskid,branch_count into parm_up_taskid,parm_branch_count from zjxf_process_task where taskid = (
       select task_id from zjxf_process_task where taskid = parm_taskid
   );
   
      if(parm_branch_count = 1) then   /** 說明沒有分支 **/
    delete from zjxf_process_task where taskid = parm_taskid;
    call delete_up_process_task_proc(parm_up_taskid);
   else                        /** 說明有分支 **/
    delete from zjxf_process_task where taskid = parm_taskid;
             update zjxf_process_task set branch_count = (parm_branch_count - 1) where taskid = parm_next_taskid;
   end if;
end;

/************刪除下級代辦的儲存過程*********************/
drop procedure IF EXISTS delete_down_process_task_proc;
create procedure delete_down_process_task_proc (parm_taskid varchar(32))
begin
  declare parm_down_taskid varchar(4000);
  declare split_value varchar(4000);
  declare split_count int;
  declare i int default 0;
  select down_taskid into parm_down_taskid from zjxf_process_task where taskid = parm_taskid;
  if(parm_down_taskid <> '' || parm_down_taskid <> null) then
    set split_count = (1+(length(parm_down_taskid) - length(replace(parm_down_taskid,',',''))));
    while i < split_count do
   set split_value = reverse(substring_index(reverse(substring_index(parm_down_taskid,',',i)),',',1));
   delete from zjxf_process_task where taskid = split_value;
   call delete_down_process_task_proc(split_value);
   set i = i+1;
    end while;
 end if;
end;