Sql递归错误列名CTE无效

时间:2013-09-23 07:23:04

标签: visual-studio-2010 sql-server-2008 c#-4.0 recursive-query

我已经完成了CTE它工作正常,但我需要得到最后一个节点的总和所以问题是当我添加T1.Debit列来计算它给我一个无效的列名'借记'!!!在线***

ALTER Function [dbo].[SubTopics_GetSum]
 -- Add the parameters for the stored procedure here
    (
    @TopicID int
    )
RETURNS TABLE 
AS
RETURN 
(
    -- Add the SELECT statement with parameter references here
    WITH cte
    AS (
        SELECT
            T1.TopicID,
            T1.Code,
            T1.Description,
            T1.ParentID,
            T1.ParentID AS NewParentID,
            CAST(T1.Code AS nvarchar(MAX)) AS TopicCode,
            CAST(T1.Description AS nvarchar(MAX)) AS TopicDescription,
            isnull((Accounting.DocumentDetail.Debit),0) AS Debit,
            isnull((Accounting.DocumentDetail.Credit),0) AS Credit
        FROM Accounting.Topics AS T1
            LEFT OUTER JOIN Accounting.DocumentDetail
                ON T1.TopicID = Accounting.DocumentDetail.TopicFK
        where NOT EXISTS(
                        SELECT
                            T2.TopicID,
                            T2.Code,
                            T2.Description,
                            T2.ParentID,
                            isnull((Accounting.DocumentDetail.Debit),0) AS Debit,
                            isnull((Accounting.DocumentDetail.Credit),0) AS Credit
                        FROM Accounting.Topics AS T2
                            LEFT OUTER JOIN Accounting.DocumentDetail
                                ON T2.TopicID = Accounting.DocumentDetail.TopicFK
                        WHERE (ParentID = T1.TopicID)
                        )
        UNION ALL
        SELECT
            c.TopicID,
            c.Code,
            c.Description,
            c.ParentID,
            T1.ParentID AS NewParentID,
            CAST(T1.Code AS nvarchar(MAX)) + c.TopicCode AS TopicCode,
            CAST(T1.Description AS nvarchar(MAX)) + ' - ' + c.TopicDescription AS TopicDescription,
            ***   isnull((T1.Debit),0)+isnull(c.Debit,0) AS Debit,--IN THIS LINE error 'Invalid Column Name 'Debit''
            isnull(c.Credit,0) AS Credit
        FROM cte AS c
            INNER JOIN Accounting.Topics AS T1
                ON T1.TopicID = c.NewParentID
        )

    SELECT isnull(sum(Debit),0)AS Debit,
        isnull(sum(Credit),0)AS Credit
    FROM cte AS c
    WHERE (NewParentID = @TopicID)
)

让我知道我的代码混淆了什么错误!!!

实际上它并没有返回我的借记卡,贷方的最后一个节点总和...... !!! 检查下面的图片

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为下面的代码会有所帮助,

ALTER FUNCTION [dbo].[Subtopics_getsum] 
-- Add the parameters for the stored procedure here 
(@TopicID INT) 
returns TABLE 
AS 
    RETURN ( 
      -- Add the SELECT statement with parameter references here 
      WITH cte 
           AS (SELECT T1.topicid, 
                      T1.code, 
                      T1.description, 
                      T1.parentid, 
                      T1.parentid                                     AS 
                      NewParentID 
                      , 
                      Cast(T1.code AS NVARCHAR(max)) 
                      AS TopicCode, 
                      Cast(T1.description AS NVARCHAR(max))           AS 
                      TopicDescription, 
                      Isnull(( accounting.documentdetail.debit ), 0)  AS Debit, 
                      Isnull(( accounting.documentdetail.credit ), 0) AS Credit 
               FROM   accounting.topics AS T1 
                      LEFT OUTER JOIN accounting.documentdetail 
                                   ON T1.topicid = 
                                      accounting.documentdetail.topicfk 
               WHERE  NOT EXISTS(SELECT T2.topicid, 
                                        T2.code, 
                                        T2.description, 
                                        T2.parentid, 
    Isnull(( accounting.documentdetail.debit ), 0) 
    AS 
    Debit, 
    Isnull(( accounting.documentdetail.credit ), 0) AS 
    Credit 
    FROM   accounting.topics AS T2 
    LEFT OUTER JOIN accounting.documentdetail 
    ON T2.topicid = 
    accounting.documentdetail.topicfk 
    WHERE  ( parentid = T1.topicid ))) 
    SELECT Isnull(Sum(debit), 0) AS Debit, 
    Isnull(Sum(credit), 0)AS Credit 
    FROM   cte AS c 
    WHERE  ( newparentid = @TopicID )
    )

答案 1 :(得分:0)

我认为您Accounting.Topics没有列Debit

<强> UPDTAE

isnull(( T1.Debit ),0)+ isnull(c.Debit,0)AS Debit, - IN THIS LINE错误'无效列名'借记''                 isnull(c.Credit,0)AS Credit             来自cte AS c                 INNER JOIN Accounting.Topics AS T1                     ON T1.TopicID = c.NewParentID

在上面的代码中,T1.Debit引用了Accounting.Topics