Solved

Splitting a value into multiple line item values using SQL

Posted on 2013-06-23
3
242 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now