Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:40:04+00:00 2026-06-14T19:40:04+00:00

This is kind of a followup to my question Reference a column and update

  • 0

This is kind of a followup to my question Reference a column and update it in the same statement. I am now trying to use a local variable and have it updated in the same update statement.

declare @tmp table (ID int primary key, UNPAID money)

insert into @tmp select 1, 31.63
insert into @tmp select 2, 49.20
insert into @tmp select 3, 99.00
insert into @tmp select 4, 41.00

declare @paymentAmmount money
select @paymentAmmount = SUM(UNPAID) from @tmp
            
declare cur_oustandingAR cursor local static for select ID  from @tmp order by ID
open cur_oustandingAR

declare @currentID int

fetch next from cur_oustandingAR into @currentID
while (@@fetch_status = 0 and @paymentAmmount > 0)
begin
    begin
        update @tmp
            set UNPAID = case when @paymentAmmount > UNPAID then 0 else UNPAID - @paymentAmmount end,
                @paymentAmmount = case when @paymentAmmount > UNPAID then @paymentAmmount - UNPAID else 0 end
            where ID = @currentID
    end
    fetch next from cur_oustandingAR into @currentID
end

select * from @tmp
select @paymentAmmount as LeftoverPayment

You can run the query here, here is the results it gives

ID          UNPAID
----------- ---------------------
1           0.00
2           0.00
3           58.00
4           41.00

LeftoverPayment 
--------------- 
0               

All of the value should of been 0 and @paymentAmmount at the end should also be 0. What is going wrong that is causing the values to not be applied correctly?


P.S. I know how to fix it, just break the one query in to the 3 following queries, but I wanted to do it as a single query so I did not need as many lookups against the real table

select @oldUnpaid = UNPAID from @tmp where ID = @currentID
update @tmp 
    set UNPAID = case when @paymentAmmount > UNPAID then 0 else UNPAID - @paymentAmmount end
    where ID = @currentID
select @paymentAmmount = case when @paymentAmmount > @oldUnpaid then @paymentAmmount - @oldUnpaid else 0 end

I just wanted to know why what I am doing currently does not work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:40:05+00:00Added an answer on June 14, 2026 at 7:40 pm
    declare @tmp table (ID int primary key, UNPAID money)
    
    insert into @tmp select 1, 31.63
    insert into @tmp select 2, 49.20
    insert into @tmp select 3, 99.00
    insert into @tmp select 4, 41.00
    
    declare @paymentAmmount money
    declare @paymentAmmountbuf money
    select @paymentAmmount = SUM(UNPAID) from @tmp
    declare cur_oustandingAR cursor local static for select ID  from @tmp order by ID
    open cur_oustandingAR
    
    declare @currentID int
    
    fetch next from cur_oustandingAR into @currentID
    while (@@fetch_status = 0 and @paymentAmmount > 0)
    begin
        begin
            select @paymentAmmountbuf=@paymentAmmount
    
            update @tmp
                set UNPAID = case when @paymentAmmountbuf > UNPAID then 0 else UNPAID - @paymentAmmountbuf end,
                    @paymentAmmount = case when @paymentAmmount > UNPAID then @paymentAmmount - UNPAID else 0 end
                where ID = @currentID
        end
        fetch next from cur_oustandingAR into @currentID
    end
    
    select * from @tmp
    select @paymentAmmount as LeftoverPayment
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is kind of a followup on my previous question . I have an
This is kind of a followup to this question . I have the following
this is some kind of followup to my last question. i needed to find
I have this kind of question. In my form, i got this as a
This question is kind of general and not very specific. We have a java
This is a followup to my own previous question and I'm kind of embarassed
This is kind of a followup to the question I had yesterday. I had
As kind of a followup to this question I've gotten a solution working on
This question is kind of a follow up to this question I asked a
This is kind of a follow-up question to this . When I'm debugging threads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.