谁能帮我解释一下这个存储过程
ALTER PROCEDURE [dbo].[AddAduitI]
@AduitType varchar(50),
@IId varchar(50),
@IMoney varchar(50),
@IPer varchar(50),
@IRID varchar(500),
@Memo varchar(500)
AS
BEGIN
declare @AduitDept varchar(50)
declare @WFType varchar(50)
declare @ADuitID as int,@num as int
declare @wfid as int
declare @AduitPerID int --审核人的ID
select @AduitDept=DeptNo from LoginPerInfoView where id=@IPer
INSERT INTO [IntentionTbl](AduitType,[IntentionID],[IntentionMoney],[IntentionPer],[IntentionReceiptID],[UpIntentionDate]
,[StatusType],[StatusTag],[Memo]) VALUES (@AduitType,@IId,@IMoney,@IPer,@IRID,GETDATE(),0,1,@Memo );
set @ADuitID=@@IDENTITY
----------------------------------------
;with DeptTree(id,DeptName,ParentId,leader,level,FlagTrashed,FlagDeleted) as
(
select id,DeptName,ParentId,leader,0 as level,FlagTrashed,FlagDeleted
from AgentDepartment
where DeptNo = @AduitDept
union all
select a.id,a.DeptName,a.ParentId,a.Leader,level + 1,a.FlagTrashed,a.FlagDeleted
from AgentDepartment a
join DeptTree b
on b.ParentId = a.id
)
select * into #DeptTree from DeptTree where FlagTrashed !=1 and FlagDeleted !=1
select @num=COUNT(1) from #DeptTree a left join LoginPerInfoView c on a.leader=c.id left join WorkFlowTbl b on (b.AduitPostion=c.PositionNo and b.AduitTypeId=@AduitType)
where a.leader is not null and leader!='' and leader!=0
if(@num>0)
begin
select top 1 @AduitPerID=leader,@wfid=b.id from #DeptTree a left join LoginPerInfoView c
on a.leader=c.id left join WorkFlowTbl b on (b.AduitPostion=c.PositionNo and b.AduitTypeId=@AduitType)
where a.leader is not null and leader!='' and leader!=0 and AduitStatus != 0 order by [level]
insert into AgentWFAduitTbl(wfAduitPerID, WFAduitTypeID,WFID,AduitStatus,AduitDatetime,AduitMemo,del)
values (@AduitPerID,@ADuitID,@WFID ,null,null,null,0)
end
else
begin
declare GetPerIDCur cursor for
select b.id,a.id from LoginPerInfoView a,WorkFlowTbl b where a.PositionNo = b.AduitPostion and b.AduitTypeid = @aduitType and AduitStatus != 0
open GetPerIDCur
FETCH NEXT FROM GetPerIDCur INTO @WFID,@AduitPerID
WHILE @@FETCH_STATUS = 0
begin
if not exists(select id from AgentWFAduitTbl where WFAduitPerID=@AduitPerID and WFAduitTypeID=@ADuitID and AduitStatus is null)
begin
if(@WFID is not null)
begin
insert into AgentWFAduitTbl(wfAduitPerID, WFAduitTypeID,WFID,AduitStatus,AduitDatetime,AduitMemo,del)
values (@AduitPerID,@ADuitID,@WFID ,null,null,null,0)
end
end
fetch next from GetPerIDCur into @WFID,@AduitPerID
end
CLOSE GetPerIDCur;
DEALLOCATE GetPerIDCur;
end
drop table #DeptTree
END
GO