我试图在SQL Server中使用MS Excel中的PMT函数来实现此功能。
当我在excel中使用
=PMT(0;3;-29590)
给我这个结果:
$9,863.33
当我尝试任何公式时,请给我除以零误差。我有3个时期的付款,没有任何利率。那我怎么解决这个问题呢?
这是实际功能:
create function dbo.PMT
(
@rate float,
@periods smallint,
@principal numeric(20,2)
)
returns numeric (38,9)
as
begin
declare @pmt numeric (38,9)
declare @WK_periods float,
@WK_principal float,
@wk_One float,
@WK_power float
select @WK_periods = @periods,
@WK_principal = @principal,
@WK_One = 1
select @pmt =
round(
( @WK_principal * (@rate*power(@WK_One+@rate,@WK_periods)))
/ (power(@WK_One+@rate,@WK_periods)-@WK_One)
,9)
return @pmt
end
答案 0 :(得分:0)
在函数中添加简单的条件
if (@rate = 0)
select @pmt = (@principal / @periods) * -1
else
select @pmt =
round(
( @WK_principal * (@rate*power(@WK_One+@rate,@WK_periods)))
/ (power(@WK_One+@rate,@WK_periods)-@WK_One)
,9)