2017-05-11 3 views
0

Ich habe 2 Tabellen:SERVER CTE SQL - unendlich Rekursivität

PLAN_Type_FIRST_ETAP_Type:

PLAN_Type_Id | ETAP_TypeFirst_Id 
----------------+--------------------- 
     1   |  5 
     2   |  9 
     3   |  8 

ETAP_TypeTransition:

PLAN_Type_Id | ETAP_Type_Id | ETAP_TypeNext_Id 
----------------+-------------------+---------------------- 
     2   |  6   |  7 
     3   |  8   |  9 
     2   |  7   |  10 
     2   |  11   |  NULL 
     3   |  9   |  8 
     2   |  10   |  11 

Für eine PLAN_Type_Id gibt es eine ETAP_TypeFirst_Id, dann finden wir es in der anderen Tabelle, dann nehmen wir die ETAP_TypeNext_Id, dann nehmen wir die ETAP_T ype_Id das ist das Gleiche von der ETAP_Type_Id.

Also haben wir etapes, die für einen Plan aufeinander folgen.

So dies zu bestellen Ich habe gemacht, dass:

WITH CTEA (id_plan, id_firstetp) 
AS 
(
    SELECT distinct p.PLAN_Type_Id as id_plan, p.ETAP_TypeFirst_Id as id_firstetp 
    from PLAN_Type_FIRST_ETAP_Type p 
    where p.ETAP_TypeFirst_Id is not null 
), 
cte (id_plan,id_firstetp, etp_next) 
AS  (SELECT c.id_plan, et.ETAP_Type_Id, et.ETAP_TypeNext_Id as etp_next 
     from ETAP_TypeTransition as et inner join CTEA as c 
     on c.id_plan=et.PLAN_Type_Id and c.id_firstetp=et.ETAP_Type_Id 
     UNION ALL 
     SELECT et.PLAN_Type_Id, et.ETAP_Type_Id, et.ETAP_TypeNext_Id 
     FROM cte inner join ETAP_TypeTransition et 
     on etp_next=et.ETAP_Type_Id and et.PLAN_Type_Id=id_plan 
     ) 
     select * from cte order by id_plan 

Das Problem ist, weil die Reihen

PLAN_Type_Id | ETAP_Type_Id | ETAP_TypeNext_Id 
----------------+-------------------+---------------------- 
     3   |  8   |  9 
     3   |  9   |  8 

ich die Nachricht habe:

L'Anweisung a été terminieren. La récursivité maximale 100 a été épuisée avant la fin de l'instruction.

Ich habe gemacht, dass, weil mein Ziel ist es in diesem Beispiel Werten zu wählen: 3 ---- ---- 8 9, die eine Schleife erzeugen. Es kann mehrere Schleifen geben (aber nur eine nach Plan).

Erwartete Ausgabe

3 ---- 8 ---- 9 

Wie kann ich das erreichen?

+0

Fügen Sie Ihre erwartete Ausgabe –

+0

es ist am Ende: 3 ---- ---- 8 9. – hedidev1

+0

Nutzung OPTION (MAXRECURSION 0) Siehe Link [http://stackoverflow.com/questions/ 15080922] (http://stackoverflow.com/questions/15080922) – SHD

Antwort

0
WITH CTEA (id_plan, id_firstetp) 
AS 
(
SELECT distinct p.PLAN_Type_Id as id_plan, p.ETAP_TypeFirst_Id as id_firstetp from PLAN_Type_FIRST_ETAP_Type p where p.ETAP_TypeFirst_Id is not null 
), 
      cte (id_plan,id_firstetp, etp_next) 
AS  (SELECT c.id_plan, et.ETAP_Type_Id, et.ETAP_TypeNext_Id as etp_next from ETAP_TypeTransition as et inner join CTEA as c 
     on c.id_plan=et.PLAN_Type_Id and c.id_firstetp=et.ETAP_Type_Id 
     UNION ALL 
     SELECT et.PLAN_Type_Id, et.ETAP_Type_Id, et.ETAP_TypeNext_Id 
     FROM cte inner join ETAP_TypeTransition et 
     on etp_next=et.ETAP_Type_Id and et.PLAN_Type_Id=id_plan 
     ) 
     select * from cte order by id_plan 
     option (maxrecursion 0) 
+0

Funktioniert nicht, kein Ende. wegen der Schleife – hedidev1

Verwandte Themen