sql server - sql - display all the instalments that i can pay -


i have table many instalments different customers. each customer has available balance.

i want display instalments customer can paid.

customer 1 available balance 400 customer 2 available balance 500

instalment table:

customer id       instalment amount   available 1                 150                   400 1                 150                   400 1                 150                   400 1                 150                   400 1                 150                   400 1                 150                   400 1                 150                   400 2                 200                   500 2                 200                   500 2                 200                   500 2                 200                   500 2                 200                   500 2                 200                   500 

below results want

customer id       instalment amount   available 1                 150                 400 1                 150                 400 2                 200                 500 2                 200                 500 

the code below not working

declare @dracavailable decimal,         @payamount decimal,         @runningtotal decimal  set @runningtotal = 0  declare rt_cursor cursor select t2.payamount, t2.dracavailable loanautopaytransactions t2  inner join loanautopaytransactions t1 on t2.lrac=t1.lrac   open rt_cursor  fetch next rt_cursor @payamount, @dracavailable  while @@fetch_status = 0  begin   set @runningtotal = @runningtotal + @payamount   if @runningtotal >= @dracavailable break   update loanautopaytransactions set  payamount=@runningtotal startdate=(select min(t2.startdate) loanautopaytransactions t2 t2.lrac=lrac)     fetch next rt_cursor @payamount, @dracavailable   end  close rt_cursor deallocate rt_cursor 

while i'm not sure want made try. output table variable changed update query or else. please give try , see if gives result want:

declare @dracavailable decimal,         @payamount decimal,         @runningtotal decimal,         @customerid int,                 @currentcustomerid int  set @runningtotal = 0  declare @outputtable table(     lrac int,     payamount decimal,     dracavailable decimal);  declare paymentcursor cursor select lrac, payamount, dracavailable loanautopaytransactions order lrac, startdate  open paymentcursor  fetch next paymentcursor @customerid, @payamount, @dracavailable  while @@fetch_status = 0  begin   if @currentcustomerid != @customerid set @runningtotal = 0   set @currentcustomerid = @customerid   print 'processing customer id: ' + cast(@customerid varchar)   set @runningtotal = @runningtotal + @payamount      if @runningtotal <= @dracavailable insert @outputtable (lrac, payamount, dracavailable) values (@customerid, @payamount, @dracavailable)    fetch next paymentcursor @customerid , @payamount, @dracavailable   end  close paymentcursor deallocate paymentcursor  -- print output table select lrac 'customer id', payamount 'instalment amount', dracavailable 'available' @outputtable 

Comments