fnFundRaiserTeamSolicitors
Return
Return Type |
---|
table |
Parameters
Parameter | Parameter Type | Mode | Description |
---|---|---|---|
@MyTeamID | int | IN | |
@RootFundraiserID | int | IN | |
@Levels | int | IN |
Definition
Copy
CREATE function [dbo].[fnFundRaiserTeamSolicitors](
@MyTeamID int,
@RootFundraiserID int,
@Levels int
)
--Sterling the @RootFundraiserID and @Levels params are now irrelevant
--we drill down as deep as the levels go
returns @retSolicitorIDs table (SolicitorID int primary key not null)
as
begin
declare @TeamLineage TABLE (
TeamID int NOT NULL,
AncestorTeamID int NULL)
insert into @TeamLineage
select @MyTeamID, @MyTeamID
insert into @TeamLineage
select team.id, team.reportstoid
from fundraiserteams team
where team.ReportsToID = @MyTeamID
WHILE EXISTS (
select T2.AncestorTeamID from @TeamLineage T1
inner join @TeamLineage T2 on T1.AncestorTeamID = T2.TeamID and t2.AncestorTeamID is not null
where not exists (select * from @TeamLineage T3 where t3.teamid = T1.TeamID and t3.AncestorTeamID = T2.AncestorTeamID))
insert into @TeamLineage select T1.TeamID, T2.AncestorTeamID
from @TeamLineage T1 inner join @TeamLineage T2 on T1.AncestorTeamID = T2.TeamID and t2.AncestorTeamID is not null
where not exists (select * from @TeamLineage T3 where t3.teamid = T1.TeamID and t3.AncestorTeamID = T2.AncestorTeamID)
insert into @retSolicitorIDs (SolicitorID)
SELECT frs.id
FROM @TeamLineage tl
INNER JOIN FundRaiserSolicitors frs on tl.TeamID = frs.TeamID
INNER JOIN ClientUsers cu on frs.ClientUsersID = cu.ID and cu.Deleted = 0
return
end