Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I'm trying to create a stored procedure that returns a balance of values taken from two different tables, can u please look at it and correct my logic or syntax error?

SQL
Create Proc [dbo].[GetAmntPaid](
@QNo int)
As
Begin
Select Quote.FullAmt - IsNull(Payments.AmountPaid ,0) As Balance from Quote, Payments 
where Quote.QuoteNo = Payments.QuoteNo
And @QNo = Quote.QuoteNo 
End 


Thank you in advance
Regards
Posted
Comments
dimpledevani 10-Sep-12 6:38am    
Quote.FullAmt can be null,Check that too
__TR__ 10-Sep-12 6:39am    
Try
Select ISNULL(Quote.FullAmt,0) - IsNull(Payments.AmountPaid ,0) As Balance from Quote, Payments
where Quote.QuoteNo = Payments.QuoteNo
And Quote.QuoteNo = @QNo
mlingo209 10-Sep-12 6:46am    
Still returns null

Hey Guys It may seem that I might have forgotten crucial piece of info the row might not exist at all in the 2nd table hence why IsNull wasn't working.

SQL
ALTER Proc [dbo].[GetAmntPaid](
@QNo int)
As
Begin
Select IsNull(Quote.FullAmt, 0.00)  - IsNull(Payments.AmountPaid ,0.00) 
from Quote
Left Join Payments on  Quote.QuoteNo = Payments.QuoteNo 
Where Quote.QuoteNo = @QNo
End 


This works perfectly
Regards
 
Share this answer
 
Comments
Maciej Los 10-Sep-12 16:39pm    
JOIN statement and ISNULL with correct numeric format (0.00) - This is it!
+5!
Maciej Los 10-Sep-12 16:41pm    
Formally - mark this answer as "solved".
Try this one :-

SQL
Create Proc [dbo].[GetAmntPaid](
@QNo int)
As
Begin
Select Quote.FullAmt - IsNull(Payments.AmountPaid ,0) As Balance from Quote, Payments
where Quote.QuoteNo = Payments.QuoteNo
And Quote.QuoteNo = @QNo
End
 
Share this answer
 
Comments
mlingo209 10-Sep-12 6:46am    
It is still returning nothing
mlingo209 10-Sep-12 10:16am    
Will it work if the input parameter qno does not exist in Payments table?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900