Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Splitting a value into multiple line item values using SQL

Posted on 2013-06-23
3
Medium Priority
?
250 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

876 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