Question : The statement terminated. The maximum recursion 100 has been exhausted before statement completion

Hi i have the below sql code and i keep getting the error   "The statement terminated. The maximum recursion 100 has been exhausted before statement completion"

Any ideas why?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
WITH cte AS (SELECT     dbo.vw_TFAM_ALL.TicketID, dbo.vw_TFAM_ALL.CC, dbo.vw_TFAM_ALL.FormType, dbo.vw_TFAM_ALL.SubmittedOn, 
                                                    dbo.vw_TFAM_ALL.AcknBy, dbo.vw_TFAM_ALL.AcknOn, dbo.vw_TFAM_ALL.SubmittedBy, dbo.vw_TFAM_ALL.Status, 
                                                    dbo.vw_TFAM_ALL.isSubmitted, dbo.vw_TFAM_ALL.IndvACF2ID, dbo.vw_TFAM_ALL.PendOrPost, dbo.vw_TFAM_ALL.NoOfEmpl, 
                                                    dbo.vw_TFAM_ALL.Business, dbo.vw_TFAM_ALL.ReqType, CASE WHEN [OneForm].[dbo].vw_TFAM_ALL.Status = 'Fulfilled' OR
                                                    [OneForm].[dbo].vw_TFAM_ALL.Status = 'Cancelled' THEN [OneForm].[dbo].[vw_TFAM_SLA_Times].SLA_in_Min ELSE NULL 
                                                    END AS SLA_Minutes, CASE WHEN [OneForm].[dbo].vw_TFAM_ALL.Status = 'Fulfilled' OR
                                                    [OneForm].[dbo].vw_TFAM_ALL.Status = 'Cancelled' THEN [OneForm].[dbo].[vw_TFAM_SLA_Times].SLA_in_Min / 1440 ELSE NULL 
                                                    END AS SLA_Days
                             FROM         dbo.vw_TFAM_SLA_Times INNER JOIN
                                                    dbo.vw_TFAM_ALL ON dbo.vw_TFAM_ALL.TicketID = dbo.vw_TFAM_SLA_Times.ticketid)



    SELECT     TicketID, CC, FormType, SubmittedOn, AcknBy, AcknOn, SubmittedBy, Status, isSubmitted, IndvACF2ID, PendOrPost, NoOfEmpl, Business, ReqType, 
                            SLA_Minutes, SLA_Days, CASE WHEN FormType = 'BuildingPass' AND SLA_Days <= 3 THEN 1 WHEN FormType = 'CancelAccess' AND 
                            SLA_Days <= 4 THEN 1 WHEN FormType = 'NameChange' AND SLA_Days <= 10 THEN 1 WHEN FormType = 'ChangeAccess' AND 
                            SLA_Days <= 10 THEN 1 WHEN FormType = 'ETCChange' AND SLA_Days <= 3 THEN 1 WHEN FormType = 'EmpStatusChange' AND 
                            SLA_Days <= 3 THEN 1 WHEN FormType = 'HomeAccess' AND ReqType = 'New Access' AND 
                            SLA_Days <= 15 THEN 1 WHEN FormType = 'HomeAccess' AND ReqType = 'Add Application' AND 
                            SLA_Days <= 15 THEN 1 WHEN FormType = 'HomeAccess' AND ReqType = 'Cancel Home Access' AND 
                            SLA_Days <= 10 THEN 1 WHEN FormType = 'HomeAccess' AND ReqType = 'Token Replacement – Lost/Damaged/Expired' AND 
                            SLA_Days <= 15 THEN 1 WHEN FormType = 'NewHire' AND SLA_Days <= 10 THEN 1 WHEN FormType = 'PassReset' AND 
                            SLA_Days <= 3 THEN 1 WHEN FormType = 'ReactivationAccess' AND SLA_Days <= 3 THEN 1 WHEN FormType = 'SharedDrive' AND 
                            SLA_Days <= 7 THEN 1 WHEN FormType = 'SuspendAccess' AND SLA_Days <= 3 THEN 1 WHEN FormType = 'TMChange' AND 
                            SLA_Days <= 5 THEN 1 WHEN FormType = 'TransferIn' AND SLA_Days <= 10 THEN 1 WHEN FormType = 'TransferOut' AND 
                            SLA_Days <= 4 THEN 1 WHEN FormType = 'PositionChange' AND SLA_Days <= 10 THEN 1 WHEN FormType = 'AccessExc' AND 
                            SLA_Days <= 10 THEN 1 WHEN FormType = 'GenericEmail' AND ReqType = 'Create New Generic Mail ID' AND 
                            SLA_Days <= 10 THEN 1 WHEN FormType = 'GenericEmail' AND ReqType = 'Delete Generic Mail ID' AND 
                            SLA_Days <= 5 THEN 1 WHEN FormType = 'GenericEmail' AND ReqType = 'Create New Distribution List' AND 
                            SLA_Days <= 7 THEN 1 WHEN FormType = 'GenericEmail' AND ReqType = 'Delete Distribution List' AND 
                            SLA_Days <= 5 THEN 1 WHEN FormType = 'GenericEmail' AND ReqType = 'Change Distribution List' AND SLA_Days <= 5 THEN 1 ELSE NULL 
                            END AS SLA_Met, 
                            CASE WHEN FormType = 'AccessExc' THEN 'Access - Exception Process' WHEN FormType = 'BuildingPass' THEN 'Building Pass' WHEN FormType
                             = 'CancelAccess' THEN 'Cancel Access' WHEN FormType = 'ChangeAccess' THEN 'Change Access' WHEN FormType = 'EmpStatusChange' THEN 'Employment Status Change'
                             WHEN FormType = 'ETCChange' THEN 'Change to ETC Admin DB' WHEN FormType = 'GenericEmail' THEN 'Generic Email' WHEN FormType = 'HomeAccess'
                             THEN 'Home Access' WHEN FormType = 'NameChange' THEN 'Name Change' WHEN FormType = 'NewHire' THEN 'New Hire' WHEN FormType = 'PassReset'
                             THEN 'Password Re-set' WHEN FormType = 'PositionChange' THEN 'Position/Role Change' WHEN FormType = 'ReactivationAccess' THEN 'Reactivation Access'
                             WHEN FormType = 'SharedDrive' THEN 'Shared Drive Access' WHEN FormType = 'SuspendAccess' THEN 'Suspend Access' WHEN FormType = 'TMChange'
                             THEN 'Team Manager Change Only' WHEN FormType = 'TransferIn' THEN 'Transfer In' WHEN FormType = 'TransferOut' THEN 'Transfer Out' END AS
                             FormName
     FROM         cte AS cte_1

Answer : The statement terminated. The maximum recursion 100 has been exhausted before statement completion

Try the code below - !! CAUTION !! - please test it thoroughly.

My guess is that CTE recursions are counted cumulatively for all recursive/embedded CTEs.  So if you have 3 levels of CTE and top level causes 2nd level to loop 50 times, 2nd level causes 3rd level to loop 80 times (80 days), you get 4000 recursions already without even looping on the top level.

Consider using while loops or other methods to reduce the number of CTE calls.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
CREATE FUNCTION [dbo].[CalcMinutesSLA_TFAM] (
 @startTime DATETIME,
 @endTime DATETIME
 ) 
returns int
as
begin
	declare @minutes int;
	declare @startPlus1 datetime
	declare @endLess1 datetime
	-- work out the range for the full days in between
	select @startPlus1 = datediff(d, 0, @startTime+1),
			@endLess1 = datediff(d, 0, @endTime-1)
	-- check how many non-weekend days exist in this range
	select @minutes = 480 * (convert(int,@endLess1-@startPlus1)/7*5 + count(*))
	from master..spt_values
	where type='P' and number between 0 and datediff(d,
		@startPlus1+convert(int,@endLess1-@startPlus1)/7*7, @endLess1)
		and datename(dw, @startPlus1+number) != 'S'
	-- check how many non-weekend holidays exist in this range
	select @minutes = @minutes - count(*)
	from web_holidays
	where HolidayDate between @startPlus1 and @endless1
	  and left(datename(dw,HolidayDate),1) != 'S'
	-- adjust times for first/last days
	if datepart(hh,@startTime) < 9 set @startTime = datediff(dd,0,@startTime)+9.0/24
	if datepart(hh,@endTime) >= 17 set @endTime = datediff(dd,0,@endTime)+17.0/24
	if @startTime >= @endTime return coalesce(@minutes, 0);
	-- if start date is not a holiday and not a weekend, add minutes
	select @minutes = @minutes +
		datediff(mi, @startTime, datediff(dd,0,@startTime)+17.0/24)
	where not exists (
		select * from web_holidays
		where HolidayDate = datediff(dd,0,@startTime))
		and left(datename(dw,@startTime),1) != 'S'
	-- if end date is not a holiday and not a weekend, add minutes
	select @minutes = @minutes +
		datediff(mi, datediff(dd,0,@endTime)+9.0/24, @endTime)
	where not exists (
		select * from web_holidays
		where HolidayDate = datediff(dd,0,@endTime))
		and left(datename(dw,@endTime),1) != 'S'
	-- if start=end, we have counted the day twice, take 480 off
	select @minutes = @minutes - 480
	where not exists (
		select * from web_holidays
		where HolidayDate = datediff(dd,0,@endTime))
		and left(datename(dw,@endTime),1) != 'S'
		and datediff(d, @startTime, @endTime) = 0 -- same day
   return coalesce(@minutes, 0);
end
GO
Random Solutions  
 
programming4us programming4us