我有一个查询返回错误,超出了最大递归级别。
我知道如何通过添加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 语句的正上方,但是在创建视图语句中时,它会给出语法错误。如果我自己运行查询,则在创建视图语句之外它可以工作