2012-03-10 10 views
88

Sigo obteniendo un error de recursión máximo con esta consulta.La recursividad máxima 100 se ha agotado antes de completar la instrucción

Al principio pensé que era porque se devolvía un nulo y luego intentaría hacer coincidir los valores nulos que causaban el error, sin embargo, reescribí mi consulta para que no se devolvieran los nulos y el error aún ocurra.

¿Cuál sería la mejor manera de reescribir esta función, por lo que el error no se producirá

WITH EmployeeTree AS 
(
    SELECT 
     EMP_SRC_ID_NR Id, USR_ACV_DIR_ID_TE Uuid, 
     CASE Employees.APV_MGR_EMP_ID 
      WHEN Null THEN '0' 
      ELSE Employees.APV_MGR_EMP_ID 
     END as ApprovalManagerId 
    FROM 
     dbo.[tEmployees] as Employees WITH (NOLOCK) 
    WHERE 
     APV_MGR_EMP_ID = @Id 
     and Employees.APV_MGR_EMP_ID is not null 
     and Employees.EMP_SRC_ID_NR is not null 

    UNION ALL 

    SELECT 
     EMP_SRC_ID_NR Id, USR_ACV_DIR_ID_TE Uuid, 
     CASE Employees.UPS_ACP_EMP_NR 
      WHEN Null THEN '1' 
      ELSE Employees.UPS_ACP_EMP_NR 
     END as ApprovalManagerId 
    FROM 
     dbo.[tEmployees] as Employees WITH (NOLOCK) 
    WHERE 
     UPS_ACP_EMP_NR = @Id 
     and Employees.APV_MGR_EMP_ID is not null 
     and Employees.EMP_SRC_ID_NR is not null 

    UNION ALL 

    SELECT 
     Employees.EMP_SRC_ID_NR, Employees.USR_ACV_DIR_ID_TE, 
     CASE Employees.APV_MGR_EMP_ID 
      WHEN Null THEN '2' 
      ELSE Employees.APV_MGR_EMP_ID 
     END 
    FROM 
     dbo.[tEmployees] as Employees WITH (NOLOCK) 
    JOIN 
     EmployeeTree ON Employees.APV_MGR_EMP_ID = EmployeeTree.Id 
    where 
     Employees.APV_MGR_EMP_ID is not null 
     and Employees.EMP_SRC_ID_NR is not null    
) 
SELECT 
    Id AS [EmployeeId], 
    Uuid AS [EmployeeUuid], 
    ApprovalManagerId AS [ManagerId] 
FROM EmployeeTree   

Respuesta

167

Especificar el maxrecursion option al final de la consulta:

... 
from EmployeeTree 
option (maxrecursion 0) 

que le permite especifique con qué frecuencia el CTE puede recurse antes de generar un error. Maxrecursion 0 permite la recursión infinita.

+0

hmm esto funcionó pero la consulta vuelto mucho más filas, entonces debe tener –

+4

@bugz MAXRECURSION 0 hace ahora afecta a su consulta, ¿tiene que buscar el problema en otro lugar –

+2

ahh era un refrence circular mis datos, gracias por la ayuda –

14

es solo una muestra para evitar el máximo error de recursividad. tenemos que usar la opción (maxrecursion 365); u opción (maxrecursion 0);

DECLARE @STARTDATE datetime; 
DECLARE @EntDt datetime; 
set @STARTDATE = '01/01/2009'; 
set @EntDt = '12/31/2009'; 
declare @dcnt int; 
;with DateList as 
( 
    select @STARTDATE DateValue 
    union all 
    select DateValue + 1 from DateList  
    where DateValue + 1 < convert(VARCHAR(15),@EntDt,101) 
) 
    select count(*) as DayCnt from ( 
    select DateValue,DATENAME(WEEKDAY, DateValue) as WEEKDAY from DateList 
    where DATENAME(WEEKDAY, DateValue) not IN ('Saturday','Sunday')  
)a 
option (maxrecursion 365); 
Cuestiones relacionadas