I have this function that given a initial and final date gives the corresponding year/month in that range:
CREATE FUNCTION [dbo].[fnYearMonth]
(
@Initial Date,
@Final Date
)
RETURNS TABLE
AS
RETURN
With dateRange(StatDate) as
(
select @Initial
union all
select dateadd(month, 1, StatDate)
from dateRange
where dateadd(month, 1, StatDate) <= CAST(DATEADD(month,DATEDIFF(month,0,@Final)+1,0)-1 as Date)
)
select DATEPART(year, StatDate) AS MyYear, DATEPART(month, StatDate) AS MyMonth From dateRange where StatDate <= @Final
The problem is that the default limit of MAXRECURSION of 100 only makes available date ranges of a maximum of 8 years and 4 months. That is insufficient.
I tried using "OPTION (MAXRECURSION 2000);" in the functions that use this function but that didn't work because I called this function in a WITH statement.
My only solution now is to turn this inline function into a multi-statement function and use "OPTION (MAXRECURSION 2000);". But I would prefer to avoid this option for performance reasons. ¿Is any other alternative?
Thanks for the help.