Home Forums SQL Server 2005 T-SQL (SS2K5) The maximum recursion 100 has been exhausted before statement completion RE: The maximum recursion 100 has been exhausted before statement completion

  • Here's an alternative that doesn't use a recursive CTE to generate the dates.

    DECLARE @d1 datetime, @d2 datetime

    DECLARE @n int

    SELECT

    @d1 = '20090831',

    @d2 = '20090904',

    @n = DATEDIFF(day,@d1,@d2)

    SELECT

    DATENAME(dw,date)

    FROM

    (

    SELECT TOP (@n)

    date = DATEADD( day,

    ROW_NUMBER()OVER (ORDER BY t1.name)-1,

    @d1)

    FROM

    sys.columns t1, sys.columns t2

    ) d

    WHERE

    DATENAME(dw,date) NOT IN ('Saturday','Sunday')

    I notice that your solution excludes the end date (@EndDt) in the count, is that intentional?

    The above solution does the same. If you need to include the end date change the line:

    @n = DATEDIFF(day,@d1,@d2)

    to

    @n = DATEDIFF(day,@d1,@d2)+1

    Alternative solutions could use either a Tally or Calendar table.