Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

How to fix this query?

ALTER FUNCTION [dbo].[Func_Bank_Balance]
(
@accountid int,
@addoneday datetime
)
Returns decimal(18,4)
AS
BEGIN
Declare @return decimal(18,4)
Declare @return1 decimal(18,4)
Declare @bal int

select @bal=a.balance from account a where a.id=@accountid
if @bal is null
set @bal = 0

select @return1 = sum(amount + countervalue) from alltransaction
where accountid=@accountid and trandatetime < @addoneday and accountid > 0  
if @return1 is null
set @return1 = 0

set @return = @bal + @return1
return @return
END


The result of
select a.balance from account a where a.id='12148'
2.82

The result of
select sum(amount + countervalue) from alltransaction where accountid='12148' and trandatetime < '2009-12-06 06:00:00' and accountid > 0
null

The result of
Select dbo.Func_Bank_Balance('12148', '2009-12-06 06:00:00') accountbal from alltransaction t where t.trandatetime < '2009-12-06 06:00:00'
2.0000

It is wrong, it should be 2.8200

How should I fix it?




ASKER CERTIFIED SOLUTION
Avatar of DaveSam
DaveSam

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
try this

Select dbo.Func_Bank_Balance(a.balance) accountbal
from alltransaction t
where t.trandatetime < '2009-12-06 06:00:00'
Avatar of techques
techques

ASKER

@bal decimal(18,4) yes, it has fixed