CREATE DEFINER=`root`@`localhost`PROCEDURE`addDep`(in depName varchar(32),in parentId int,in enabled boolean,outresultint,out result2 int) begin declare did int; declare pDepPath varchar(64); insertinto department setname=depName,parentId=parentId,enabled=enabled; selectrow_count() intoresult; selectlast_insert_id() into did; set result2=did; select depPath into pDepPath from department whereid=parentId; update department set depPath=concat(pDepPath,'.',did) whereid=did; update department set isParent=truewhereid=parentId; end$$
CREATE DEFINER=`root`@`localhost`PROCEDURE`deleteDep`(in did int,outresultint) begin declare ecount int; declare pid int; declare pcount int; selectcount(*) into ecount from employee where departmentId=did; if ecount>0 then set result=-1; else select parentId into pid from department whereid=did; deletefrom department whereid=did and isParent=false; selectrow_count() intoresult; selectcount(*) into pcount from department where parentId=pid; if pcount=0 then update department set isParent=falsewhereid=pid; endif; endif; end$$