Solved

Splitting a value into multiple line item values using SQL

Posted on 2013-06-23
3
245 Views
Last Modified: 2013-07-07
I have a receipts table which stores the amounts collected for a particular episode id.

and have billdetail table which stores the individual line order items.

My requirment is as below

whe i update the receipt table, i want to split the  total amount collected to the corresponding line items and adjust the balance leftover amount (if any) to the last row of billdetail table.

I am using the cursor  which solves  partially my problem  as long as total billamount is less than receipt amount. if it is more than the bill amount it is updating the balance amt to the last row in bill detail  table. can some expert suggest the solution pls.

I am enclosing below the  cursor i am using for updataion.

   Declare  C_Update Cursor local FAST_FORWARD   For
     
     Select Caseid,Adviseserialno,NVal from dbo.Trn_BillDetail where Caseid = @Caseid
   
     Open C_Update
     
     Fetch next From C_Update into @Caseid,@srno,@NVal
         
                   While (@@FETCH_STATUS = 0)
                 
                        Begin
                               If @Nval > 0 and @Nval <= @AmtRecd  
                              
                                       Begin
                                          update dbo.Trn_BillDetail  Set   Amtpaid = @Nval             
                                                                                      Where Trn_BillDetail.Caseid = @Caseid
                                                                                      and Trn_BillDetail.AdviseSerialno = @srno                                                                               
                  
                                          set @AmtRecd = @AmtRecd - @Nval       
                                    End                   
                                    
                               else if  @NVal > @AmtRecd
                              
                                    Begin
                                          update dbo.Trn_BillDetail  Set   Amtpaid = @AmtRecd            
                                                                                      Where Trn_BillDetail.Caseid = @Caseid
                                                                                      and Trn_BillDetail.AdviseSerialno = @srno
                                                                                     
                                       
                              
                                       
                                          set @AmtRecd =  @Nval- @AmtRecd       
                                    End                   
                              
                                  
                        Fetch Next from C_Update into @Caseid,@srno,@NVal
                        
                        End
   Close c_update
   Deallocate c_update
0
Comment
Question by:venkataramanaiahsr
  • 2
3 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39270332
if you need a good solution to your problem,

can you do the below

give us the create scripts for your table,
the sample data via the insert scripts

the expected outcome....
0
 

Accepted Solution

by:
venkataramanaiahsr earned 0 total points
ID: 39295492
Thanks for your response.   using   rowcount and if logic  i could solve the problem
0
 

Author Closing Comment

by:venkataramanaiahsr
ID: 39305116
Thanks for your response.   using   rowcount and if logic inside the cursor  i could solve the problem
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question