【问题标题】:TSQL Maxrecursion on a ctecte 上的 TSQL Maxrecursion
【发布时间】:2012-11-03 23:39:47
【问题描述】:

我收到此错误:

最大递归100已经用完之前的语句 完成

当我运行这个函数时:

    WITH allDays AS (

        SELECT @DateEarly AS date

        UNION ALL

        SELECT DATEADD(dd, 1, date) as date
        FROM allDays s  
        WHERE DATEADD(dd, 1, date) <= @DateLate


   )
    SELECT *
    from allDays 
    where dbo.isFestivo(date)>0

我试图附加这个选项:

OPTION (MAXRECURSION 200);

但我在“选项”字之前收到错误 156。 你知道为什么吗?

【问题讨论】:

  • 具体在哪里追加这个OPTION (MAXRECURSION 200) ???你能告诉我们你有这个选项的查询吗?

标签: sql-server tsql stored-procedures sql-server-2012 sql-server-2012-express


【解决方案1】:

您可能将选项放在错误的位置。它需要在 where 之后

WITH allDays AS (
    SELECT @DateEarly AS date
    UNION ALL
    SELECT DATEADD(dd, 1, date) as date
    FROM allDays s  
    WHERE DATEADD(dd, 1, date) <= @DateLate
)
SELECT *
from allDays 
where dbo.isFestivo(date)>0
option (maxrecursion 200);

但是试试这个。会更快...

select DATEADD(d, number, @dateearly) as [date]
from master..spt_values 
where type='p'
and number<=datediff(d,@dateearly,@datelate)
and dbo.isFestivo(date)>0

【讨论】:

  • 第二个有效(但我不得不用 dbo.isFestivo(DATEADD(d, number, @dateearly)) 更改 dbo.isFestivo(date))!
  • 第二种方式很危险,因为spt_values 未记录的 Microsoft 内容可能有一天会被弃用和删除...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 1970-01-01
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
相关资源
最近更新 更多