The statement terminated. The maximum recursion 100 has been exhausted before statement completion.http://forums.asp.net/t/1594634.aspx/1?The+statement+terminated+The+maximum+recursion+100+has+been+exhausted+before+statement+completion+Mon, 02 Jul 2012 12:21:00 -040015946344042834http://forums.asp.net/p/1594634/4042834.aspx/1?The+statement+terminated+The+maximum+recursion+100+has+been+exhausted+before+statement+completion+The statement terminated. The maximum recursion 100 has been exhausted before statement completion. <p>Hi All</p> <p>I have the below sql scripts that give me the error:<br> &quot;The statement terminated. The maximum recursion 100 has been exhausted before statement completion.&quot;</p> <p>Ive tried using OPTION ( MAXRECURSION 0 ) or 1000 but it still&nbsp;&nbsp;give me the same error.</p> <p></p> <pre class="prettyprint">WITH cte as (SELECT ticketid, setstatus, setdate, row_number() OVER (partition BY (ticketid) ORDER BY setdate ASC) AS rn FROM [OneForm].[dbo].[TFAM_SLA_Times] UNION SELECT TicketID, 'Submitted', SubmittedON, 0 FROM [OneForm].[dbo].vw_TFAM_ALL)</pre><pre class="prettyprint"> SELECT a.ticketid, SUM(OneForm.dbo.[CalcMinutesSLA_TFAM](a.setdate, b.setdate)) AS SLA_in_Min FROM vw_TFAM_SLA_Combined AS a LEFT JOIN vw_TFAM_SLA_Combined AS b ON a.ticketid = b.ticketid AND a.rn = b.rn - 1 WHERE (a.SetStatus = 'Submitted' OR a.SetStatus = 'Acknowledged' OR a.SetStatus = 'Pending') GROUP BY a.TicketID</pre><p><br></p><p><br></p><p>If i remove the call to the function [CalcMinutesSLA_TFAM] or the b.rn - 1 it works fine.</p><p><br></p><p>Here is the code for the function:</p><p><br></p><p></p><pre class="prettyprint">CREATE FUNCTION [dbo].[CalcMinutesSLA2] ( @startTime DATETIME, @endTime DATETIME ) returns int as begin declare @minutes int; -- you should only need to alter these two to change work times declare @wdStartMins int, @wdLength int set @wdStartMins = 510; -- 8.5 hours from 00:00 or 8:30 am set @wdLength = 510; -- 8.5 hours (e.g., 8:30 am - 5:00 pm) declare @fwd datetime, @lwd datetime set @fwd = dateadd(mi, @wdStartMins, datediff(d, 0, @startTime)); if (@fwd &lt; @startTime) set @fwd = @startTime; set @lwd = dateadd(mi, @wdStartMins+@wdLength, datediff(d, 0, @endTime)); if (@lwd &gt; @endTime) set @lwd = @endTime; ;with cte(st, et) as ( select @fwd , case datediff(d, @fwd, @lwd) when 0 then @lwd else dateadd(mi, @wdStartMins+@wdLength, datediff(d, 0, @fwd)) end union all select dateadd(mi, @wdStartMins, datediff(d, 0, st+1)) , case datediff(d, st+1, @lwd) when 0 then @lwd else dateadd(mi, @wdStartMins+@wdLength, datediff(d, 0, st+1)) end from cte where dateadd(mi, @wdStartMins, datediff(d, 0, st+1)) &lt; @lwd ) select @minutes = sum(datediff(mi, st, et)) from cte C LEFT JOIN web_holidays H ON CONVERT(varchar,C.st,101) = H.HolidayDate where left(datename(dw, st), 1) &lt;&gt; 'S' and st &lt; et and H.HolidayDate IS NULL ; return coalesce(@minutes, 0); end</pre> <p><br> <br> </p> <p></p> <p></p> 2010-08-24T14:59:53-04:004043015http://forums.asp.net/p/1594634/4043015.aspx/1?Re+The+statement+terminated+The+maximum+recursion+100+has+been+exhausted+before+statement+completion+Re: The statement terminated. The maximum recursion 100 has been exhausted before statement completion. <p>Hi,</p> <p>Please refer following</p> <p><a target="_self" href="http://michaeljswart.com/?tag=recursive-cte">http://michaeljswart.com/?tag=recursive-cte</a><br> </p> <p><a target="_self" href="http://sqlserverpedia.com/wiki/CTE_-_Recursive_CTE_Internals">http://sqlserverpedia.com/wiki/CTE_-_Recursive_CTE_Internals</a></p> <p>hope this helps<br> </p> <p><br> </p> 2010-08-24T16:29:09-04:004043097http://forums.asp.net/p/1594634/4043097.aspx/1?Re+The+statement+terminated+The+maximum+recursion+100+has+been+exhausted+before+statement+completion+Re: The statement terminated. The maximum recursion 100 has been exhausted before statement completion. <p>Thanks for the reply ketan_al</p> <p>Those two article only explain what recursion is which i know but i just cant fix the current code i have to avoid that error.</p> <p><br> </p> <p>Any suggestions?</p> 2010-08-24T17:21:47-04:004044027http://forums.asp.net/p/1594634/4044027.aspx/1?Re+The+statement+terminated+The+maximum+recursion+100+has+been+exhausted+before+statement+completion+Re: The statement terminated. The maximum recursion 100 has been exhausted before statement completion. <p>Hi,</p> <p>I tried start and end date where end date is one day after start date, and it seem no error.</p> <p>Please provide&nbsp;some sample input and data to simulate the&nbsp;error.&nbsp;</p> <p>E.g.<br> a) Holiday table data<br> b) Start and end date</p> 2010-08-25T03:51:00-04:004045098http://forums.asp.net/p/1594634/4045098.aspx/1?Re+The+statement+terminated+The+maximum+recursion+100+has+been+exhausted+before+statement+completion+Re: The statement terminated. The maximum recursion 100 has been exhausted before statement completion. <p>finnaly fixed it.</p> <p>i ended up using the MAXRECURSION just before i was putting in the wrong place, i had to use it in the function.</p> 2010-08-25T14:15:22-04:005049167http://forums.asp.net/p/1594634/5049167.aspx/1?Re+The+statement+terminated+The+maximum+recursion+100+has+been+exhausted+before+statement+completion+Re: The statement terminated. The maximum recursion 100 has been exhausted before statement completion. <p>thank birader....</p> 2012-07-02T12:21:00-04:00