Running balance amount in column "Balance"

Muhammad Ali 1 Reputation point
2022-01-18T10:16:59.567+00:00

I have a table having Columns "Dr", "Cr" and "Balance"

What to write in Procedure in SQL Developer for taking running balance amount in column "Balance" as under:

Account DR CR BALANCE
XY Co. 200 200
XY Co. 400 -200
XY Co. 300 100

Regards

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

1 answer

Sort by: Most helpful
  1. LiHong-MSFT 10,041 Reputation points
    2022-01-19T02:47:23.693+00:00

    Hi,@Muhammad Ali
    Please check this:

    create table #test(Account nvarchar(30),  
                           DR money,  
        				   CR money,  
        				   Balance money  
        				  )  
    insert into #test values('XY Co.',200,200,null),('XY Co.',400,-200,null),('XY Co.',300,100,null)  
    select * from #test  
          
    IF OBJECT_ID('proc_test')IS NOT NULL   
    DROP PROC proc_test;  
    GO   
    CREATE PROC proc_test  
    AS  
    update #test  
    set Balance=DR-CR  
          
    EXEC proc_test  
    select * from #test  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    0 comments No comments