La ricorsione massima 100 è stata esaurita prima del completamento dell'istruzione


136

Continuo a ottenere un max recursion errorcon questa query.

All'inizio ho pensato che fosse perché veniva restituito un null e poi avrebbe cercato di abbinare i valori null causando l'errore, ho riscritto la mia query in modo che i null non vengano restituiti e l'errore si verifica ancora.

Quale sarebbe il modo migliore per riscrivere questa funzione, in modo che l'errore non si verifichi

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        

Questa linea potrebbe essere sostituita da COALESCE(): CASE Employees.APV_MGR_EMP_ID WHEN Null THEN '0' ELSE Employees.APV_MGR_EMP_ID END as ApprovalManagerId=COALESCE(Employees.APV_MGR_EMP_ID, 0) AS ApprovalManagerID
— David Faber

Risposte:


249

Specificare l' opzione maxrecursion alla fine della query:

...
from EmployeeTree
option (maxrecursion 0)

Ciò consente di specificare la frequenza con cui il CTE può ricorrere prima di generare un errore. Maxrecursion 0 consente una ricorsione infinita.


1
hmm ha funzionato ma la query ha restituito molte più righe di quelle che avrebbe dovuto
— HELP_ME

5
@bugz Maxrecursion 0 ora non influisce sulla tua query, devi cercare il problema altrove
— t-clausen.dk

6
ahh è stato un riferimento circolare ai miei dati, grazie per l'aiuto
— HELP_ME

3
+1 Ho usato questa opzione per eseguire il debug di un problema simile. Se la query è infinitamente ricorsiva, è necessario annullare la query in Management Studio dopo averla eseguita o il server eseguirà lo spooling delle righe fino a quando il client esaurisce la memoria.
— Iain Samuel McLean Elder

1
Sebbene ciò possa risolvere il problema in situazioni in cui si desidera che la query venga esaminata in modo molto approfondito, potrebbe essere solo mascherare un problema nella query.
— Christian Findlay,

24

è solo un esempio per evitare il massimo errore di ricorsione. dobbiamo usare l' opzione (maxrecursion 365); o opzione (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);
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.