Maxrecursion和Column导致溢出错误

时间:2014-12-31 13:57:44

标签: tsql sql-server-2012

此查询出现最大递归错误。

WITH [Days] AS
(   
    SELECT DATENAME(weekday,getdate()) AS NameOfDay
          ,DAY(GETDATE()) AS NumberOfDay
          ,GETDATE() AS curDate
    UNION ALL
    SELECT DATENAME(WEEKDAY,DATEADD(day, -1, curDate)) As NameOfDay
          ,DAY(DATEADD(DAY, -1, curDate)) AS NumberOfDay
          ,DATEADD(DAY, -1, curDate) AS curDate
    FROM [Days]
    WHERE DAY(GETDATE()) - DAY(DATEADD(DAY, -1, curDate)) >= 0
)
SELECT NameOfDay
FROM [Days]  
ORDER BY NumberOfDay 

,错误是:

  

声明终止。在语句完成之前,最大递归100已经用尽。

当我向查询添加option (maxrecursion 0)时,出现此错误:

  

向'datetime'列添加值会导致溢出。

我不知道如何解决这些错误。任何帮助将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

SET DATEFIRST 7;
GO
WITH [Days] AS
(   
    SELECT DATENAME(WEEKDAY, GETDATE()) AS NameOfDay
          ,DAY(GETDATE()) AS NumberOfDay
          ,GETDATE() AS curDate
    UNION ALL
    SELECT DATENAME(WEEKDAY,DATEADD(day, -1, curDate)) As NameOfDay
          ,DAY(DATEADD(DAY, -1, curDate)) AS NumberOfDay
          ,DATEADD(DAY, -1, curDate) AS curDate
    FROM [Days]
    WHERE DATEPART(WEEKDAY, DATEADD(DAY, -1, curDate)) >= 2
)
SELECT NameOfDay
FROM [Days]