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.
| 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 planswithin this i have two UDF's - one to calculate the interest rate based on years invested - worksone to calculate the final return - worksI now need to create a procedure to compare a couple of marketing ideasHere is what I have come up with - it is not workingthe errors areMsg 16916, Level 16, State 1, Procedure InvestmentPlans, Line 104A cursor with the name 'custcurspr' does not exist.Msg 8114, Level 16, State 5, Procedure InvestmentPlans, Line 108Error 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)ASDECLARE @R FLOAT -- rate of interstDECLARE @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 typeDECLARE @Custid char(4) -- customer id DECLARE @Custname char(15) -- customer name -- Declare cursor Declare custCursor CURSOR local Scroll Keyset OptimisticFOR SELECT Custid,CustName,FinalAmount,NumberofYears,DepositType from @invest Order By custid--open the cursorOPEN custCursorFETCH Next FROM custCursor INTO @custid,@custname,@P,@N,@deptypeWhile @@FETCH_STATUS =0 BEGINIf @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 BEGINSELECT @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 custCursorDEALLOCATE custcursprDECLARE @cust as bestscenarioInsert 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 |
|
|
suki_66
Starting Member
12 Posts |
Posted - 2010-03-30 : 06:38:21
|
| This is the message I keep gettingMsg 8114, Level 16, State 5, Procedure InvestmentPlans, Line 114Error converting data type varchar to float.I have looked at this over and over and just cant seem to find the difference |
 |
|
|
suki_66
Starting Member
12 Posts |
Posted - 2010-03-30 : 16:30:48
|
| I finally figured it out - so this procedure worksBUT it is not what was needed....I misunderstood so back to the drawing board. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
suki_66
Starting Member
12 Posts |
Posted - 2010-03-31 : 03:37:02
|
Here is the entire stored procedurePROC [dbo].[InvestmentPlans](@invest as bestScenario READONLY)ASDECLARE @R FLOAT -- rate of interstDECLARE @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 planDeclare @deptype char(6) -- deposit typeDECLARE @Custid char(4) -- customer id DECLARE @Custname char(15) -- customer name -- Declare cursor Declare custCursor CURSOR local Scroll Keyset OptimisticFOR SELECT Custid,CustName,Principal,NumberofYears,DepositType from @invest --open the cursorOPEN custCursorFETCH Next FROM custCursor INTO @custid,@custname,@P,@N,@deptypeWhile @@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 custCursorDEALLOCATE custCursorDECLARE @cust as bestscenarioInsert 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 @custthere are a few glitches that i would still like to resolve1. it gives 310 results and really should only test this 20 times.....so somewhere is a loop issue2. it gives a math error in lines 74, 90 and 92 i think. |
 |
|
|
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. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|
|
|