Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Coverting a VB Function to a Stored Procedure

Author  Topic 

kpburke
Starting Member

3 Posts

Posted - 2002-03-08 : 11:24:21
Help ! I need to convert a VB function to a stored procedure. Unfortuneaty my knowledge of both VB and SQL is not up to the task.

Anyone here care to try ?

Here's the VB function

Original VB code
'Rev 231, APR now returns one decimal place
Function AprRate(ByVal Capital As Double, _
ByVal Balloon As Double, _
ByVal Rental As Double, _
ByVal Term As Integer, _
ByVal MthsAdv As Integer, _
ByVal FirstMth As Integer, _
ByVal PayFreq As Integer, ByVal ExtraPay As Integer)

Dim XPOWER As Double, XPOW2 As Double, Guess As Double, GuessChange As Double
Dim xNum As Double, XDEN As Double, RealAPR As Double
Dim Periodno As Integer, Count As Long
Dim FirstPay As Long, LastPay As Long, T1 As Long


FirstPay = FirstMth - 1
If FirstPay < 0 Then FirstPay = 0

T1 = MthsAdv - FirstPay
If T1 < 0 Then T1 = 0
LastPay = Term + ExtraPay - T1

MthsAdv = MthsAdv + 1
LastPay = LastPay - 1


Guess = 1
Count = 0
If PayFreq = 0 Then PayFreq = 1

Do
If Rental = 0 Then Exit Do
Count = Count + 1
If Count > 99 Then
Exit Do
End If
xNum = 0
XDEN = 0
For Periodno = 1 To Term
XPOWER = Guess ^ (Periodno - 1)
XPOW2 = XPOWER * Guess
If Periodno = 1 And MthsAdv > 0 Then
xNum = xNum + Rental * MthsAdv / PayFreq * XPOW2
XDEN = XDEN + Rental * Periodno * XPOWER
End If
If Periodno >= FirstMth Then
If Periodno <= LastPay Then
If (Periodno - FirstMth + 1) / PayFreq = Fix((Periodno - FirstMth + 1) / PayFreq) Or ExtraPay <> 0 Then
xNum = xNum + Rental * XPOW2
XDEN = XDEN + Rental * (Periodno) * XPOWER
End If
End If
End If
Next Periodno

XPOWER = Guess ^ (Term - 1)
XPOW2 = XPOWER * Guess
xNum = xNum + Balloon * XPOW2
XDEN = XDEN + Balloon * Term * XPOWER

xNum = xNum - Capital

GuessChange = xNum / XDEN
Guess = Guess - GuessChange
If Abs(GuessChange) < 0.0000001 Then
Exit Do
End If
Loop

If Count > 99 Or Rental = 0 Then
AprRate = 0
Else
RealAPR = (1 / Guess) ^ 12 - 1
AprRate = Int(RealAPR * 1000 + 0.5) / 10
End If
End Function

Good Luck ;)

yakoo
Constraint Violating Yak Guru

312 Posts

Posted - 2002-03-08 : 13:17:41
Try this out


CREATE PROCEDURE sp_AprRate (
@Capital As Float,
@Balloon As Float,
@Rental As Float,
@Term As SmallInt,
@MthsAdv As SmallInt,
@FirstMth As SmallInt,
@PayFreq As SmallInt,
@ExtraPay As SmallInt
)

AS

DECLARE @XPOWER As Float, @XPOW2 As Float, @Guess As Float, @GuessChange As Float
DECLARE @xNum As Float, @XDEN As Float, @RealAPR As Float
DECLARE @Periodno As SmallInt, @Count As Int
DECLARE @FirstPay As Int, @LastPay As Int, @T1 As Int

SELECT @FirstPay = @FirstMth - 1
If @FirstPay < 0
SELECT @FirstPay = 0

SELECT @T1 = @MthsAdv - @FirstPay

If @T1 < 0
SELECT @T1 = 0

SELECT @LastPay = @Term + @ExtraPay - @T1, @MthsAdv = @MthsAdv + 1, @LastPay = @LastPay - 1


SELECT @Guess = 1, @Count = 0
If @PayFreq = 0
SELECT @PayFreq = 1

WHILE 1 = 1 BEGIN
IF @Rental = 0
BREAK

SELECT @Count = @Count + 1

IF @Count > 99
BREAK

SELECT @xNum = 0, @XDEN = 0

SELECT @Periodno = 1
WHILE @Periodno <= @Term BEGIN

SELECT @XPOWER = POWER(@Guess, (@Periodno - 1))
SELECT @XPOW2 = @XPOWER * @Guess
IF @Periodno = 1 And @MthsAdv > 0 BEGIN
SELECT @xNum = @xNum + @Rental * @MthsAdv / @PayFreq * @XPOW2, @XDEN = @XDEN + @Rental * @Periodno * @XPOWER
END
IF @Periodno >= @FirstMth BEGIN
IF @Periodno <= @LastPay BEGIN
IF (@Periodno - @FirstMth + 1 / @PayFreq) = (Sign((@Periodno - @FirstMth + 1) / @PayFreq) * Cast(Abs((@Periodno - @FirstMth + 1) / @PayFreq) as integer)) OR @ExtraPay != 0 BEGIN
SELECT @xNum = @xNum + @Rental * @XPOW2
SELECT @XDEN = @XDEN + @Rental * (@Periodno) * @XPOWER
END
END
END
SELECT @Periodno = @Periodno + 1
END

SELECT @XPOWER = POWER(@Guess, (@Term - 1))
SELECT @XPOW2 = @XPOWER * @Guess
SELECT @xNum = @xNum + @Balloon * @XPOW2
SELECT @XDEN = @XDEN + @Balloon * @Term * @XPOWER

SELECT @xNum = @xNum - @Capital

SELECT @GuessChange = @xNum / @XDEN
SELECT @Guess = @Guess - @GuessChange
IF Abs(@GuessChange) < 0.0000001 BEGIN
BREAK
END
END

If @Count > 99 Or @Rental = 0 BEGIN
RETURN SELECT 0
END ELSE BEGIN
SELECT @RealAPR = POWER((1 / @Guess), 12) - 1
RETURN SELECT Cast(((@RealAPR * 1000 + 0.5) / 10) as smallint)
END

GO


Go to Top of Page
   

- Advertisement -