CTE递归不起作用

时间:2013-05-06 07:34:56

标签: sql sql-server sql-server-2008

WITH CTE
AS
(
    SELECT CONVERT(VARCHAR(100), 'MILESTONE')  AS NAME, CASEDEPENDENCYMILESTONE.TEMPLATEMILESTONEID,   CASEDEPENDENCYMILESTONE.DEPENDENTTEMPLATEMILESTONEID, 
                          CASEDEPENDENCYMILESTONE.DEPENDENTTEMPLATETASKID,MILESTONETIME, CASETEMPLATEMILESTONE.PROJECTEDENDDATE
    FROM         CASETEMPLATEMILESTONE INNER JOIN 
                          CASEDEPENDENCYMILESTONE ON CASETEMPLATEMILESTONE.CASETEMPLATEMILESTONEID = CASEDEPENDENCYMILESTONE.TEMPLATEMILESTONEID
    WHERE     (CASETEMPLATEMILESTONE.CASETIMELINEID = @CASETIMELINEID AND 
    TEMPLATEMILESTONEID=@MILESTONEID) --AND CASEDEPENDENCYMILESTONE.DEPENDENTTEMPLATEMILESTONEID<>0
    UNION ALL
    SELECT  CONVERT(VARCHAR(100), 'MILESTONE') AS NAME, CASEDEPENDENCYMILESTONE.TEMPLATEMILESTONEID, CASEDEPENDENCYMILESTONE.DEPENDENTTEMPLATEMILESTONEID, 
                          CASEDEPENDENCYMILESTONE.DEPENDENTTEMPLATETASKID,c.MILESTONETIME, c.PROJECTEDENDDATE
                           FROM CASEDEPENDENCYMILESTONE INNER JOIN 
                           CTE ON CASEDEPENDENCYMILESTONE.TEMPLATEMILESTONEID=CTE.DEPENDENTTEMPLATEMILESTONEID
                           inner join CASETEMPLATEMILESTONE c on c.CASETEMPLATEMILESTONEID=CASEDEPENDENCYMILESTONE.TEMPLATEMILESTONEID
                           where c.CASETIMELINEID = @CASETIMELINEID
    UNION ALL
    SELECT  CONVERT(VARCHAR(100),'TASK') AS NAME, CASEDEPENDENCYTASK.TEMPLATETASKID, CASEDEPENDENCYTASK.DEPENDENTTEMPLATEMILESTONEID, 
                          CASEDEPENDENCYTASK.DEPENDENTTEMPLATETASKID,TASKTIME, t.PROJECTEDENDDATE
                           FROM CASEDEPENDENCYTASK INNER JOIN 
                           CTE ON CASEDEPENDENCYTASK.TEMPLATETASKID=CTE.DEPENDENTTEMPLATETASKID
                           inner join casetemplatetask t  on t.CaseTemplateTaskId=CASEDEPENDENCYTASK.TEMPLATETASKID
                         where t.CASETIMELINEID = @CASETIMELINEID 
)

SELECT * FROM CTE option(maxrecursion 0)

但我没有得到任何结果。此功能运行5分钟。

0 个答案:

没有答案