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 2008 Forums
 Transact-SQL (2008)
 help with stored procedure

Author  Topic 

suki_66
Starting Member

12 Posts

Posted - 2010-03-30 : 00:04:02
I need to create a stored procedure to compare two investment plans
within this i have two UDF's - one to calculate the interest rate based on years invested - works
one to calculate the final return - works

I now need to create a procedure to compare a couple of marketing ideas
Here is what I have come up with - it is not working


the errors are
Msg 16916, Level 16, State 1, Procedure InvestmentPlans, Line 104
A cursor with the name 'custcurspr' does not exist.
Msg 8114, Level 16, State 5, Procedure InvestmentPlans, Line 108
Error converting data type varchar to float.

I am not even sure this is doing what i need but if i could get the errors fixed I could further evaluate it.

CREATE PROC InvestmentPlans
(@invest as bestScenario READONLY)
AS
DECLARE @R FLOAT -- rate of interst
DECLARE @N int -- number of years
DECLARE @P FLOAT --Priniciple amount
DECLARE @Cplan FLOAT -- Current paln final amount
declare @Nplan float -- proposed plan final amount
DECLARE @Results FLOAT -- holds results of
Declare @deptype char(15) -- deposit type
DECLARE @Custid char(4) -- customer id
DECLARE @Custname char(15) -- customer name

-- Declare cursor
Declare custCursor CURSOR
local Scroll Keyset Optimistic

FOR SELECT Custid,CustName,FinalAmount,NumberofYears,DepositType from @invest Order By custid

--open the cursor
OPEN custCursor

FETCH Next FROM custCursor INTO @custid,@custname,@P,@N,@deptype
While @@FETCH_STATUS =0
BEGIN

If @N>=20.0 set @R=15
else if @N>=11.0 set @R=12.5
else if @N>=6.0 set @R=10
else if @N>=4.0 set @R=7.5
else set @R=5

-- to check for saving customers
if @deptype = 'Saving'

-- calculate compound innterest for saving customers for plan 1
BEGIN
set @Nplan= @P * (Power (@N,(1 + @R)))
set @Cplan = @P + (@P * @N * @R)
SET @RESULTS =(((@Nplan-@Cplan)/@Cplan)*100)

--check to see if proposed plan @APROP is > current @ACURR
IF @RESULTS >= 10
BEGIN
SELECT @custid AS CUST ,@deptype AS DESPOSITTYPE,'Plan 1 suitable'
END
ELSE
BEGIN
SELECT @CUSTID AS CUST,@deptype AS DESPOSITTYPE,'Plan 1NOT suitable,the difference is,',@RESULTS,'%'
END
-- check for saving customers for plan 2
set @Nplan= @P + (@P * @N * (@R +2))
SET @Cplan = @P + (@P * @N * @R)
SET @RESULTS =(((@Nplan-@Cplan)/@Cplan)*100)
--compare proposed vs current for > 10% rule for plan 2
IF @RESULTS >= 10
BEGIN
SELECT @custid AS CUST,@deptype AS DESPOSITTYPE,'PLAN 2 SUITABLE'
END
ELSE
BEGIN
SELECT @custid AS CusT,@deptype AS DESPOSITTYPE,'NOT suitable,the difference is,',@RESULTS,'%'
END
END
-- to check for Fixed customers

if @deptype = 'Fixed'


-- Increase R by 2 % for fixed for plan 3

BEGIN
set @Nplan= @P * @N * (@R +2)
SET @Cplan = @P * (Power (@N,(1 + @R)))
SET @RESULTS =(((@Nplan-@Cplan)/@Cplan)*100)

-- compare proposed vs plan for plan 3

IF @RESULTS >= 10
BEGIN
SELECT @custid AS CUST,@deptype AS DESPOSITTYPE,'Plan 3 SUITABLE'
END
ELSE
BEGIN
SELECT @custid AS CusT,@deptype AS DESPOSITTYPE,'Plan 3 not suitable,the difference is,',@RESULTS,'%'
END

-- Calculate fixed plans with 5% bonus for plan 4

SET @Cplan = @P * (Power (@N,(1 + @R)))
set @Nplan= @P * (Power (@N,(1 + @R))) + (.05 *(( @P * (Power (@N,(1 + @R))))* @N))
SET @RESULTS =(((@Nplan-@Cplan)/@Cplan)*100)

-- compare proposed vs current for >10% for plan 4

IF @RESULTS >= 10
BEGIN
SELECT @custid AS CUST,@deptype AS DESPOSITTYPE,'Plan 4 SUITABLE'
END
ELSE
BEGIN
SELECT @custid AS CusT,@deptype AS DESPOSITTYPE,'Plan 4 not suitable,the difference is,',@RESULTS,'%'
END
END

FETCH Next FROM custCursor INTO @custid,@custname,@P,@N,@deptype
END

close custCursor
DEALLOCATE custcurspr
DECLARE @cust as bestscenario


Insert into @cust values(1,'aaa',100000,2,'Saving'),(2,'bbb',5000,5,'Fixed'),(3,'ccc',8200,4,'Saving'),
(4,'ddd',15000,10,'Fixed'),(5,'eee',6500,20,'Fixed');



exec InvestmentPlans @cust

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-30 : 00:21:41
The error is here: DEALLOCATE custcurspr

It needs to be custCursor instead.

Tbe other error is because your INSERT statement is incorrect. Use one INSERT/VALUES for each set.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

suki_66
Starting Member

12 Posts

Posted - 2010-03-30 : 06:38:21
This is the message I keep getting
Msg 8114, Level 16, State 5, Procedure InvestmentPlans, Line 114
Error converting data type varchar to float.

I have looked at this over and over and just cant seem to find the difference
Go to Top of Page

suki_66
Starting Member

12 Posts

Posted - 2010-03-30 : 16:30:48
I finally figured it out - so this procedure works
BUT it is not what was needed....I misunderstood so back to the drawing board.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-30 : 16:39:24
Show us lines 110 until the end of the procedure.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

suki_66
Starting Member

12 Posts

Posted - 2010-03-31 : 03:37:02
Here is the entire stored procedure

PROC [dbo].[InvestmentPlans]
(@invest as bestScenario READONLY)
AS
DECLARE @R FLOAT -- rate of interst
DECLARE @N INT -- number of years
DECLARE @P FLOAT --Priniciple amount
DECLARE @Cplan FLOAT -- Current plan final amount
declare @Nplan FLOAT -- proposed plan final amount
DECLARE @Results FLOAT -- holds results of new plan
Declare @deptype char(6) -- deposit type
DECLARE @Custid char(4) -- customer id
DECLARE @Custname char(15) -- customer name

-- Declare cursor
Declare custCursor CURSOR
local Scroll Keyset Optimistic

FOR SELECT Custid,CustName,Principal,NumberofYears,DepositType from @invest

--open the cursor
OPEN custCursor

FETCH Next FROM custCursor INTO @custid,@custname,@P,@N,@deptype
While @@FETCH_STATUS =0
BEGIN

If @N>=20.0 set @R=15
else if @N>=11.0 set @R=12.5
else if @N>=6.0 set @R=10
else if @N>=4.0 set @R=7.5
else set @R=5

-- to check for saving customers
if @deptype = 'Saving'

-- calculate compound interest for saving customers::plan 1
BEGIN
SET @Nplan= @P * (Power (@N,(1 + @R)))
SET @Cplan = @P + (@P * @N * @R)
--SET @RESULTS =(((@Nplan-@Cplan)/@Cplan)*100)

--check to see if proposed plan @Nplan is > current @Cplan
IF @Nplan > @Cplan *1.1
BEGIN
SELECT @custid AS CUST ,@deptype AS DESPOSITTYPE,'Plan 1 suitable'
END
ELSE
BEGIN
SELECT @CUSTID AS CUST,@deptype AS DESPOSITTYPE,'Plan 1 is not suitable,the difference is,',@RESULTS,'%'
END
-- check for saving customers for plan 2
SET @Nplan= @P + (@P * @N * (@R +2))
SET @Cplan = @P + (@P * @N * @R)
--SET @RESULTS =((@Nplan-@Cplan)/@Cplan)*100
--compare proposed vs current for > 10% rule for plan 2
IF @Nplan > @Cplan *1.1

BEGIN
SELECT @custid AS CUST,@deptype AS DESPOSITTYPE,'PLAN 2 SUITABLE'
END
ELSE
BEGIN
SELECT @custid AS CusT,@deptype AS DESPOSITTYPE,'NOT suitable,the difference is,',@RESULTS,'%'
END
END

-- to check for Fixed customers
if @deptype = 'Fixed'

-- Increase R by 2 % for fixed for plan 3

BEGIN
SET @Nplan= @P * @N * (@R +2)
SET @Cplan = @P * (Power (@N,(1 + @R)))
--SET @RESULTS =(((@Nplan-@Cplan)/@Cplan)*100)

-- compare proposed vs plan for plan 3

IF @Nplan > @Cplan *1.1
BEGIN
SELECT @custid AS CUST,@deptype AS DESPOSITTYPE,'Plan 3 SUITABLE'
END
ELSE
BEGIN
SELECT @custid AS CusT,@deptype AS DESPOSITTYPE,'Plan 3 not suitable,the difference is,',@RESULTS,'%'
END

-- Calculate fixed plans with 5% bonus for plan 4

SET @Cplan = @P * (Power (@N,(1 + @R)))
set @Nplan= @P * (Power (@N,(1 + @R))) + (.05 *(( @P * (Power (@N,(1 + @R))))* @N))
--SET @RESULTS =(((@Nplan-@Cplan)/@Cplan)*100)

-- compare proposed vs current for >10% for plan 4

IF @Nplan > @Cplan *1.1
BEGIN
SELECT @custid AS CUST,@deptype AS DESPOSITTYPE,'Plan 4 SUITABLE'
END
ELSE
BEGIN
SELECT @custid AS CusT,@deptype AS DESPOSITTYPE,'Plan 4 not suitable,the difference is,',@RESULTS,'%'
END
END

FETCH Next FROM custCursor INTO @custid,@custname,@P,@N,@deptype
END

CLOSE custCursor
DEALLOCATE custCursor


DECLARE @cust as bestscenario


Insert into @cust values('1','aaa',100000,2,'Saving')
Insert into @cust values ('2','bbb',5000,5,'Fixed')
Insert into @cust values ('3','ccc',8200,4,'Saving')
Insert into @cust values ('4','ddd',15000,10,'Fixed')
Insert into @cust values ('5','eee',6500,20,'Fixed')



exec InvestmentPlans @cust



there are a few glitches that i would still like to resolve
1. it gives 310 results and really should only test this 20 times.....so somewhere is a loop issue
2. it gives a math error in lines 74, 90 and 92 i think.

Go to Top of Page

yaraRaha
Starting Member

8 Posts

Posted - 2010-04-14 : 15:58:03

sql server dont support calling store procedure in function.why?
please help me.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-14 : 16:07:44
Why would you hijack a thread that has nothing to do with the original topic?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -