SQL 服务器:最大递归 100 在语句完成之前已用完
SQL Server : the maximum recursion 100 has been exhausted before statement completion
我有一个查询返回错误,超出了最大递归级别。
我知道如何通过在查询中添加 OPTION (maxrecursion 0)
来解决这个问题,但是,我已经尝试在查询中的不同位置添加它,但我找不到语法有效的位置。
任何人都可以告诉我在我看来需要插入查询提示的位置吗?
/****** Object: View [dbo].[SiconCFMContractLinesDetailByDayView] Script Date: 16/12/2016 12:02:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[SiconCFMContractLinesDetailByDayView]
AS
WITH dateRange as
(
SELECT [Date] = DATEADD(dd, 1, DATEADD(dd, -1,[SiconCFMContractLinesOutstandingView].[NextDueDate])),
[Frequency] = [SiconCFMContractLinesOutstandingView].[FrequencyValue],
[EndDate] = DATEADD(yy,5, [SiconCFMContractLinesOutstandingView].[NextDueDate]),
[SiconCFMContractLinesOutstandingView].[SiconContractLineID]
FROM [SiconCFMContractLinesOutstandingView]
WHERE DATEADD(mm, [SiconCFMContractLinesOutstandingView].[FrequencyValue], [SiconCFMContractLinesOutstandingView].[NextDueDate]) < DATEADD(mm, [SiconCFMContractLinesOutstandingView].[FrequencyValue], DATEADD(yy,5, [SiconCFMContractLinesOutstandingView].[NextDueDate]))
UNION ALL
SELECT DATEADD(mm, [Frequency], [Date]) [Date],
[Frequency],
[EndDate],
[SiconContractLineID]
FROM dateRange
WHERE DATEADD(mm, [Frequency], [Date]) < DATEADD(mm, [Frequency],[EndDate])
)
SELECT
(
SELECT CASE
WHEN dbo.fnSiconCFMGetSettingValue('UseAverageTimeToPay') = 'True'
THEN
CASE [SiconCFMSLCustomerAverageTimeToPayView].[AvgTimeToPayDateLastUpdated]
WHEN NULL THEN dbo.fnSiconCFMDateByPaymentTerms([SLCustomerAccount].[SYSPaymentTermsBasisID], [SLCustomerAccount].[PaymentTermsInDays], dateRange.[Date])
ELSE DATEADD([DD],[SiconCFMSLCustomerAverageTimeToPayView].[Days],dateRange.[Date])
END
ELSE dbo.fnSiconCFMDateByPaymentTerms([SLCustomerAccount].[SYSPaymentTermsBasisID], [SLCustomerAccount].[PaymentTermsInDays], dateRange.[Date])
END
)
AS [DueDate],
[StockItem].[Name] AS [Title],
[SiconCFMContractLinesOutstandingView].[Description] AS [Description],
[SiconCFMContractLinesOutstandingView].[UnitBillCoverPriceIncDisc] AS [Value],
[SiconCFMContractLinesOutstandingView].[SiconContractID],
[SiconCFMContractLinesOutstandingView].[SiconContractLineID],
[SiconCFMContractLinesOutstandingView].[SiconContractLineID] AS [ForecastDateForeignID],
'SiconContractLine' AS [ForecastDateSource],
(
SELECT
CASE WHEN EXISTS
(
SELECT [SiconCFMMemo].[SiconCFMMemoID]
FROM [SiconCFMMemo]
WHERE [SiconCFMMemo].[Deleted]=0
AND [SiconCFMMemo].[IsActive]=1
AND [SiconCFMMemo].[MemoSource]='SiconContractLine'
AND [SiconCFMMemo].[MemoForeignID]=[SiconCFMContractLinesOutstandingView].[SiconContractLineID]
)
THEN 1
ELSE 0
END
) AS [HasMemos]
FROM dateRange
INNER JOIN [SiconCFMContractLinesOutstandingView]
ON dateRange.[SiconContractLineID]
= [SiconCFMContractLinesOutstandingView].[siconContractLineID]
INNER JOIN [StockItem]
ON [StockItem].[ItemID]
= [SiconCFMContractLinesOutstandingView].[ItemID]
INNER JOIN [SLCustomerAccount]
ON [SLCustomerAccount].[SLCustomerAccountID]
= [SiconCFMContractLinesOutstandingView].[SLCustomerAccountID]
INNER JOIN [SiconCFMSLCustomerAverageTimeToPayView]
ON [SiconCFMSLCustomerAverageTimeToPayView].[SLCustomerAccountID]
= [SLCustomerAccount].[SLCustomerAccountID]
GO
按照建议,我已将 OPTION (maxrecursion 0) 添加到最后一个 GO 语句的正上方,但是在 create view 语句中它给出了语法错误。如果我 运行 查询本身,在 create view 语句之外它工作
通常,在使用递归 cte 的 select 语句的末尾。
但是,在一个不起作用的视图中。
快速搜索让我找到 this post - 这解释了正确的方法。
原来你不能在视图中使用查询提示,但你可以而且应该在调用视图的查询中使用它。
样本table:
CREATE TABLE T
(
id int,
parent int
)
INSERT INTO T VALUES (1, NULL), (2, 1), (3, 1), (4, 2), (5, 2), (6, 3), (7, 4), (8, 5);
GO
创建视图:
CREATE VIEW V
AS
WITH CTE AS
(
SELECT id, parent
FROM T
WHERE parent IS NULL
UNION ALL
SELECT t.id, t.parent
FROM T
INNER JOIN CTE ON t.parent = cte.id
)
SELECT *
FROM CTE
GO
执行视图:
SELECT *
FROM V
OPTION (MAXRECURSION 2);
我用一个非常好的脚本 check foreign keys recursively by Ann Lewkowicz
但是,我曾经有一个有很多约束的数据库,包括一些表中的外键约束。
虽然 运行 上面的脚本出现以下错误:
The statement terminated. The maximum recursion 32767 has been
exhausted before statement completion.
然后我使用 top
限制了脚本的输出,一切正常
select top 100 *
from recurseWithFields
--OPTION (MAXRECURSION 32767);
我有一个查询返回错误,超出了最大递归级别。
我知道如何通过在查询中添加 OPTION (maxrecursion 0)
来解决这个问题,但是,我已经尝试在查询中的不同位置添加它,但我找不到语法有效的位置。
任何人都可以告诉我在我看来需要插入查询提示的位置吗?
/****** Object: View [dbo].[SiconCFMContractLinesDetailByDayView] Script Date: 16/12/2016 12:02:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[SiconCFMContractLinesDetailByDayView]
AS
WITH dateRange as
(
SELECT [Date] = DATEADD(dd, 1, DATEADD(dd, -1,[SiconCFMContractLinesOutstandingView].[NextDueDate])),
[Frequency] = [SiconCFMContractLinesOutstandingView].[FrequencyValue],
[EndDate] = DATEADD(yy,5, [SiconCFMContractLinesOutstandingView].[NextDueDate]),
[SiconCFMContractLinesOutstandingView].[SiconContractLineID]
FROM [SiconCFMContractLinesOutstandingView]
WHERE DATEADD(mm, [SiconCFMContractLinesOutstandingView].[FrequencyValue], [SiconCFMContractLinesOutstandingView].[NextDueDate]) < DATEADD(mm, [SiconCFMContractLinesOutstandingView].[FrequencyValue], DATEADD(yy,5, [SiconCFMContractLinesOutstandingView].[NextDueDate]))
UNION ALL
SELECT DATEADD(mm, [Frequency], [Date]) [Date],
[Frequency],
[EndDate],
[SiconContractLineID]
FROM dateRange
WHERE DATEADD(mm, [Frequency], [Date]) < DATEADD(mm, [Frequency],[EndDate])
)
SELECT
(
SELECT CASE
WHEN dbo.fnSiconCFMGetSettingValue('UseAverageTimeToPay') = 'True'
THEN
CASE [SiconCFMSLCustomerAverageTimeToPayView].[AvgTimeToPayDateLastUpdated]
WHEN NULL THEN dbo.fnSiconCFMDateByPaymentTerms([SLCustomerAccount].[SYSPaymentTermsBasisID], [SLCustomerAccount].[PaymentTermsInDays], dateRange.[Date])
ELSE DATEADD([DD],[SiconCFMSLCustomerAverageTimeToPayView].[Days],dateRange.[Date])
END
ELSE dbo.fnSiconCFMDateByPaymentTerms([SLCustomerAccount].[SYSPaymentTermsBasisID], [SLCustomerAccount].[PaymentTermsInDays], dateRange.[Date])
END
)
AS [DueDate],
[StockItem].[Name] AS [Title],
[SiconCFMContractLinesOutstandingView].[Description] AS [Description],
[SiconCFMContractLinesOutstandingView].[UnitBillCoverPriceIncDisc] AS [Value],
[SiconCFMContractLinesOutstandingView].[SiconContractID],
[SiconCFMContractLinesOutstandingView].[SiconContractLineID],
[SiconCFMContractLinesOutstandingView].[SiconContractLineID] AS [ForecastDateForeignID],
'SiconContractLine' AS [ForecastDateSource],
(
SELECT
CASE WHEN EXISTS
(
SELECT [SiconCFMMemo].[SiconCFMMemoID]
FROM [SiconCFMMemo]
WHERE [SiconCFMMemo].[Deleted]=0
AND [SiconCFMMemo].[IsActive]=1
AND [SiconCFMMemo].[MemoSource]='SiconContractLine'
AND [SiconCFMMemo].[MemoForeignID]=[SiconCFMContractLinesOutstandingView].[SiconContractLineID]
)
THEN 1
ELSE 0
END
) AS [HasMemos]
FROM dateRange
INNER JOIN [SiconCFMContractLinesOutstandingView]
ON dateRange.[SiconContractLineID]
= [SiconCFMContractLinesOutstandingView].[siconContractLineID]
INNER JOIN [StockItem]
ON [StockItem].[ItemID]
= [SiconCFMContractLinesOutstandingView].[ItemID]
INNER JOIN [SLCustomerAccount]
ON [SLCustomerAccount].[SLCustomerAccountID]
= [SiconCFMContractLinesOutstandingView].[SLCustomerAccountID]
INNER JOIN [SiconCFMSLCustomerAverageTimeToPayView]
ON [SiconCFMSLCustomerAverageTimeToPayView].[SLCustomerAccountID]
= [SLCustomerAccount].[SLCustomerAccountID]
GO
按照建议,我已将 OPTION (maxrecursion 0) 添加到最后一个 GO 语句的正上方,但是在 create view 语句中它给出了语法错误。如果我 运行 查询本身,在 create view 语句之外它工作
通常,在使用递归 cte 的 select 语句的末尾。
但是,在一个不起作用的视图中。
快速搜索让我找到 this post - 这解释了正确的方法。
原来你不能在视图中使用查询提示,但你可以而且应该在调用视图的查询中使用它。
样本table:
CREATE TABLE T
(
id int,
parent int
)
INSERT INTO T VALUES (1, NULL), (2, 1), (3, 1), (4, 2), (5, 2), (6, 3), (7, 4), (8, 5);
GO
创建视图:
CREATE VIEW V
AS
WITH CTE AS
(
SELECT id, parent
FROM T
WHERE parent IS NULL
UNION ALL
SELECT t.id, t.parent
FROM T
INNER JOIN CTE ON t.parent = cte.id
)
SELECT *
FROM CTE
GO
执行视图:
SELECT *
FROM V
OPTION (MAXRECURSION 2);
我用一个非常好的脚本 check foreign keys recursively by Ann Lewkowicz
但是,我曾经有一个有很多约束的数据库,包括一些表中的外键约束。
虽然 运行 上面的脚本出现以下错误:
The statement terminated. The maximum recursion 32767 has been exhausted before statement completion.
然后我使用 top
限制了脚本的输出,一切正常
select top 100 *
from recurseWithFields
--OPTION (MAXRECURSION 32767);