ALTER FUNCTION [PtOrderDetailID]
(@OrderDetail_ID INT)
RETURNS INT
AS
BEGIN
DECLARE @OriginalOrderDetail_ID AS INT
SET @OriginalOrderDetail_ID = @OrderDetail_ID
IF (SELECT COUNT(1) FROM TRRawDetail OD (NOLOCK)
WHERE OD.OrderDetail_ID = @OriginalOrderDetail_ID
AND OD.RelatedOrderDetail_ID IS NOT NULL
AND OD.RelatedOrderDetail_ID <> 0) > 0
SET @OriginalOrderDetail_ID = (SELECT TOP 1 OD.RelatedOrderDetail_ID
FROM TRRawDetail OD (NOLOCK)
WHERE OD.OrderDetail_ID = @OriginalOrderDetail_ID
AND OD.RelatedOrderDetail_ID IS NOT NULL
AND OD.RelatedOrderDetail_ID <> 0)
SET @OriginalOrderDetail_ID = PtOrderDetailID(@OriginalOrderDetail_ID)
RETURN @OriginalOrderDetail_ID;
END
GO
该函数是从存储过程中调用的,但会引发“最大限制(32)”错误。请帮助解决此问题
第217条消息,第16级,状态1,第200行
超过最大存储过程,函数,触发器或视图嵌套级别(限制32)
由于这个原因,我认为这是在嵌套递归调用内部调用的函数。该如何解决?
答案 0 :(得分:0)
看一下您的函数,即使没有relatedorder_detail,您似乎也正在递归调用它,在这种情况下,它将继续使用最初传递的OrderDetail_ID递归调用。您应该将递归调用放在IF块中
ALTER FUNCTION [PtOrderDetailID]
(@OrderDetail_ID INT)
RETURNS INT
AS
BEGIN
DECLARE @OriginalOrderDetail_ID AS INT
SET @OriginalOrderDetail_ID = @OrderDetail_ID
IF (SELECT COUNT(1) FROM TRRawDetail OD (NOLOCK)
WHERE OD.OrderDetail_ID = @OriginalOrderDetail_ID
AND OD.RelatedOrderDetail_ID IS NOT NULL
AND OD.RelatedOrderDetail_ID <> 0) > 0
BEGIN
SET @OriginalOrderDetail_ID = (SELECT TOP 1 OD.RelatedOrderDetail_ID
FROM TRRawDetail OD (NOLOCK)
WHERE OD.OrderDetail_ID = @OriginalOrderDetail_ID
AND OD.RelatedOrderDetail_ID IS NOT NULL
AND OD.RelatedOrderDetail_ID <> 0)
SET @OriginalOrderDetail_ID = PtOrderDetailID(@OriginalOrderDetail_ID)
END
RETURN @OriginalOrderDetail_ID;
END
GO