Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2009
    Posts
    10

    Unanswered: update @var=col=new_value

    i have table Account(operationtype,value,date), where operationtype = deposit or withdrawal.
    after each operation performed, i want to display the current amount of money in the account.
    i already solved this in 2 ways, using a cursor and a select stmnt, which both work fine, but i got stuck when i tried to solve it using an update and now it bugs me that i can't figure it out.

    my update is:

    DECLARE @amntMoney money
    SET @amntMoney = 0
    UPDATE Account
    SET @amntMoney = value = @amntMoney + (CASE WHEN operationtype = 'deposit' THEN value ELSE -value END)

    this would update the 'value' column in the table to display the current amount of money in the account; the problem is that i don't know how or where to place an 'order by date clause', so that the update stmnt takes the records in order, from the first date to the last date in the table.

    'll provide some test data:

    (withdraw,70,2-1-2010)
    (deposit,100,1-1-2010)
    (deposit,50,1-3-2010)


    results:

    (deposit,100,1-1-2010)
    (deposit,150,1-3-2010)
    (withdraw,80,2-1-2010)

    any ideas on ho to proceed?

    thank u

  2. #2
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    If you are using SQL Server 2005 or later, you could use the OUTPUT clause.

  3. #3
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    always weary of db solutions that depend on "order", but could account be a teamp table with an appropriate cluster somewhere?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  4. #4
    Join Date
    Mar 2009
    Posts
    10
    Quote Originally Posted by Thrasymachus View Post
    always weary of db solutions that depend on "order", but could account be a teamp table with an appropriate cluster somewhere?
    yes, someone came up with this solution here:

    update @var=col=new_value

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •