title: Unable to exit Sql cursor
body: <p>This is hard for me to explain but I have tried my best to make it as easy for you all as I can. </p>
  
  <p>I have two tables. One stores information about customer <code>tbl_installations</code>. Another his internet bills <code>tbl_bills</code> (this one is more important).</p>
  
  <p><strong>tbl_installations</strong></p>
  
  <pre><code> CREATE TABLE [dbo].[tbl_installations](
          [SrNo] [int] IDENTITY(1000,1) NOT NULL,
          [name] [varchar](200) NULL,
          [email] [varchar](100) NULL,
          [phone] [varchar](20) NULL,
          [plandesc] [varchar](50) NULL,
          [plancost] [money] NULL,
          [amtpaid] [money] NULL,
          [amtbalance] [money] NULL,
          [address] [varchar](500) NULL,
          [referencename] [varchar](20) NULL,
          [installationdate] [date] NULL,
          [planduration] [int] NULL,
          [MkrId] [varchar](20) NULL,
          [MkrDt] [datetime] NULL,
          [FromDate] [datetime] NULL,
          [ToDate] [datetime] NULL,
          [ExpiryDate] [datetime] NULL,
          [Status] [varchar](20) NULL,
          [CustomerId] [varchar](20) NULL,
          [ServiceTax] [money] NULL,
          [TotalAmt] [money] NULL
      ) ON [PRIMARY]
  </code></pre>
  
  <p><strong>tbl_bills</strong></p>
  
  <pre><code>   CREATE TABLE [dbo].[tbl_Bills](
          [Srno] [int] IDENTITY(1,1) NOT NULL,
          [CustomerId] [varchar](20) NULL,
          [PlanDuration] [int] NULL,
          [Amount] [money] NULL,
          [PaidAmt] [money] NULL,
          [discount] [money] NULL,
          [PlanDesc] [varchar](20) NULL,
          [FromDate] [datetime] NULL,
          [ToDate] [datetime] NULL,
          [MkrId] [varchar](20) NULL,
          [MkrDt] [datetime] NULL,
          [ServiceTax] [money] NULL,
          [TotalAmt] [money] NULL,
          [PendingAmt]  AS ([TotalAmt]-([PaidAmt]+[discount])),
          [PaidStatus] [varchar](10) NULL
      ) ON [PRIMARY]
  </code></pre>
  
  <p><strong>THIS SCRIPT WILL GIVE YOU DATA THAT I HAVE:</strong></p>
  
  <pre><code>     insert into tbl_installations(name,email,phone,plandesc,plancost,amtpaid,amtbalance,address,referencename,installationdate,planduration,MkrId,
       MkrDt,FromDate,ToDate,ExpiryDate,Status,CustomerId,ServiceTax,TotalAmt)
       values('Gulzar','asdasd','3242342',null,null,null,null,'asda asd aada','ref name',GETDATE()-4,1,'arbaaz', GETDATE(),null,null,
       null,'INSTALLED','C2002',null,null )
  
       insert into tbl_bills (PlanDuration,Amount,PaidAmt,discount,PlanDesc,FromDate,ToDate,PaidStatus,MkrId,MkrDt,ServiceTax,TotalAmt,CustomerId)                      
       values(1,800,600,0,'1MB',GETDATE()-4,DATEADD(month,1, GETDATE()-4),'PAID','Arbaaz',getdate(),800*(12.36/100),800+800*(12.36/100),'C2001')   
  </code></pre>
  
  <p><code>PendingAmt</code> in tbl_bills is a computer column which calculates the pending amount based on <code>totalamt - (paidamt+discount)</code></p>
  
  <p><code>tbl_bills.Amount</code>  is the subsription cost eg: 800
  <code>tbl_bills.TotalAmt</code>  is sum of Amount+ServiceTax</p>
  
  <p><strong>Scenario</strong></p>
  
  <p><strong>First Month:</strong></p>
  
  <p>Customer's subsription cost(Amount) is 800
  PaidAmt=600
  ServiceTax=98.88
  TotalAmt=898.88
  PendingAmt=298.88(computed field)</p>
  
  <p><strong>Second Month:</strong></p>
  
  <p>Customer still has same subscription worth 800 (total 898.88)
  But he pays 1100 so that he settles some of pending amount. 
  This 1100 must first settle all the previous pending amounts first(by updating <code>tbl_bills.paidamt</code> and then what ever is left should be inserted as a brand new row in the same table.</p>
  
  <p><strong>HERE IS MY FAILED ATTEMPT :</strong></p>
  
  <p>THIS KEEPS ON RUNNING ENDLESSLY </p>
  
  <pre><code>declare @srno as int=0              
  declare @Cid as  varchar(20)='C2001'
  declare @PlanDuration as int=1         
  declare @Amount as money=800             
  declare @PlanDesc as varchar(20)='1MB'
  declare @FromDate as datetime ='2014-11-11'              
  declare @ToDate as datetime='2014-12-11'              
  declare @PaidStatus as varchar(20)              
  declare @MkrId as varchar(20)='arbaaz'       
  declare @status as varchar(20)='PAID'  
  declare @Discount as money=0
  declare @PaidAmt as money=1100              
  --as              
  if(@status='INSTALLED')        
  BEGIN         
   insert into tbl_bills (PlanDuration,Amount,PaidAmt,discount,PlanDesc,FromDate,ToDate,PaidStatus,MkrId,MkrDt,ServiceTax,TotalAmt,CustomerId)                      
   values(@PlanDuration,@Amount,@paidamt,@discount,@plandesc,@FromDate,@ToDate,'PAID','Arbaaz',getdate(),@Amount*(12.36/100),@Amount+@Amount*(12.36/100),@Cid)                      
  
  
   if((select sum(PendingAmt) from tbl_Bills where CustomerId=@cid)=0 )  
   begin  
   update tbl_installations set Status='PAID' ,ExpiryDate=@ToDate where CustomerId=@Cid        
   end  
   else  
   begin  
   update tbl_installations set Status='UNPAID' ,ExpiryDate=@ToDate where CustomerId=@Cid        
   end  
  END        
  ELSE        
   BEGIN        
  
     declare @pAmt as money   
   select  @pAmt=sum(PendingAmt) from tbl_Bills where CustomerId=@Cid  
  
   declare @Amt as money =@Amount  
   select @Amt=@Amt  
  
   --select  srno from tbl_Bills where CustomerId=@Cid and PendingAmt&lt;&gt;0 order by ToDate  
  
   if(@pAmt&gt;0)  
   BEGIN------------------------  
   DECLARE @ColExpir datetime  
  DECLARE @ColFallprotec datetime  
  DECLARE @CurSrno int  
  --------------------------------------------------------  
  DECLARE @MyCursor CURSOR  
  SET @MyCursor = CURSOR FAST_FORWARD  
  FOR  
   select  srno from tbl_Bills where CustomerId=@Cid and PendingAmt&lt;&gt;0 order by todate  
  OPEN @MyCursor  
  FETCH NEXT FROM @MyCursor  
  INTO @CurSrno  
  WHILE @@FETCH_STATUS = 0  
  BEGIN  
  IF(@Amt&gt;0)  
  BEGIN  
  declare @PendingAtCurSrno money   
  select @PendingAtCurSrno=pendingamt from tbl_Bills where SrNo=@CurSrno  
  print @CurSrno  -----
  print @PendingAtCurSrno ----
  if(@Amt&gt;@PendingAtCurSrno)  
  begin  
  
  update tbl_Bills set PaidAmt=TotalAmt where SrNo=@CurSrno  
  select @Amt=@Amt-@PendingAtCurSrno  
  print '1st'----
  print @amt----
      if(@amt=0)
      begin
      CLOSE @MyCursor  --tried break and return here too
  DEALLOCATE @MyCursor  --
      end
  end  
  else  
  begin  
  update tbl_Bills set PaidAmt=paidamt+@Amt where SrNo=@CurSrno  
  select @Amt=0  
  print '2nd'
  print @amt----
      if(@amt=0)
      begin
          CLOSE @MyCursor  --tried break and return here too
  DEALLOCATE @MyCursor  
      end
  end  
  END  
  END  
  
  
  FETCH NEXT FROM @MyCursor  
  INTO @PendingAtCurSrno  
  END  
  CLOSE @MyCursor  
  DEALLOCATE @MyCursor  
  END-------------------------  
  if(@Amt&gt;0)  
  begin  
   insert into tbl_bills (PlanDuration,Amount,PaidAmt,discount,PlanDesc,FromDate,ToDate,PaidStatus,MkrId,MkrDt,ServiceTax,TotalAmt,CustomerId)                      
   values(@PlanDuration,@Amount,@Amt,@discount,@plandesc,@FromDate,@ToDate,'PAID','Arbaaz',getdate(),@Amount*(12.36/100),@Amount+@Amount*(12.36/100),@Cid)                      
  end  
  
    if((select sum(PendingAmt) from tbl_Bills where CustomerId=@cid)=0 )  
   begin  
   update tbl_installations set Status='PAID' ,ExpiryDate=@ToDate where CustomerId=@Cid        
   end  
   else  
   begin  
   update tbl_installations set Status='UNPAID' ,ExpiryDate=@ToDate where CustomerId=@Cid        
   end             
  </code></pre>
  
  <p>Not only does it keep running endlessly , it keeps updating the <code>paidamt</code> over and over again and never gets to the point where it should insert another row with remaining amount.</p>