What is wrong with this CTE

RogerSchlueter-7899 1,321 Reputation points
2021-07-28T16:05:40.677+00:00

I'm pretty new to CTE's so I hope the problem is obvious with this one:

WITH cteTotal(TransactionTotal) AS
    (
    SELECT
        SUM(Amount) as TransactionTotal
    FROM
        #NewDetails
    )
    UPDATE
        FinancialAccounts
    SET
        Balance = Balance + cteTotal.TransactionTotal
    WHERE
        AccountID = @AccountID;

The error message I get is

The multi-part identifier "cteTotal.TransactionTotal" could not be bound.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,640 questions
{count} votes

Accepted answer
  1. Ryan Abbey 1,181 Reputation points
    2021-07-28T22:21:38.897+00:00

    You need to bring the CTE in to your query.

    Just to be sure #NewDetails will only contain details for one transaction? Or at least to only 1 AccountID?

    You can do away with the CTE

    update financialaccounts set balance = balance + (select sum(amount) from #NewDetails) where accountid = @AccountID

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Viorel 117.6K Reputation points
    2021-07-28T16:43:50.757+00:00

    Try something like this:

    WITH cteTotal(TransactionTotal) AS
    (
         SELECT
             SUM(Amount) as TransactionTotal
         FROM
             #NewDetails
    )
    UPDATE
             fa
    SET
             Balance = Balance + cteTotal.TransactionTotal
    from FinancialAccounts fa, cteTotal
    WHERE
             AccountID = @AccountID;
    
    1 person found this answer helpful.
    0 comments No comments

  2. MelissaMa-MSFT 24,201 Reputation points
    2021-07-29T02:38:15.767+00:00

    Hi @RogerSchlueter-7899 ,

    Welcome to Microsoft Q&A!

    The multi-part identifier "cteTotal.TransactionTotal" could not be bound.

    You could consider cteTotal as a temporary table and you also need to include the cteTotal in the update statement, otherwise the sql server could not recognize where TransactionTotal is from.

    Please also refer below:

     WITH cteTotal(TransactionTotal) AS  
         (  
         SELECT  
             SUM(Amount) as TransactionTotal  
         FROM  
             #NewDetails  
         )  
         UPDATE  
             FinancialAccounts  
         SET  
             Balance = Balance + (select TransactionTotal from cteTotal)  
         WHERE  
             AccountID = @AccountID;  
    

    Best regards,
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.