Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Splitting a value into multiple line item values using SQL

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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, …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

730 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