Sql server 比率为零时的PMT函数excel公式

Sql server 比率为零时的PMT函数excel公式,sql-server,excel,financial,Sql Server,Excel,Financial,我试图在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

我试图在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

在函数中添加一个简单的if

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)

在没有任何利率的情况下,付款只是
本金/期间*-1
,因此在其中放入
IF
,检查
@rate
是否为
0
,并进行简单的除法。