Solved

Splitting a value into multiple line item values using SQL

Posted on 2013-06-23
3
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

635 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