| Author |
Topic |
|
suki_66
Starting Member
12 Posts |
Posted - 2010-03-29 : 11:40:11
|
| I am trying to create a procedure to calculate the interest rate - i am new at this so am having trouble getting started - here is what i have so far:Cust tableCustID CustName PrincipleAmount NumberOfYears DepositTypeI have created a function that sets the principle rateCREATE FUNCTION [dbo].[IntRate](@NumberofYears decimal(7,4)) RETURNS decimal(7,4)ASBEGIN DECLARE @rate decimal(7,4) if @NumberofYears>=20 set @rate =.15 else if @NumberofYears >= 11 set @rate =.125 else if @NumberofYears>= 6 set @rate =.10 else if @NumberofYears>= 4 set @rate =.075 else if @NumberofYears>= 1 set @rate =.05 else set @rate = 0 RETURN @rateENDI want to use the following formula:SELECT case DepositType when 'saving' then NumberOfYears * PrincipleAmount * [dbo].[IntRate](NumberofYears)AS rate else PrincipleAmount * (1 + [ dbo].[IntRate](NumberofYears)AS rate) * NumberOfYearsend FROM CustomersPROBLEMI get this error:Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'AS'.Can someone please tell me what i am doing wrong. FROM CustomersWhen I do this I get this error: |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-29 : 11:47:19
|
try this:SELECT case DepositType when 'saving' then NumberOfYears * PrincipleAmount * [dbo].[IntRate](NumberofYears)else PrincipleAmount * (1 + [ dbo].[IntRate](NumberofYears)) * NumberOfYearsend AS Rate Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
suki_66
Starting Member
12 Posts |
Posted - 2010-03-29 : 11:55:01
|
| oops, Actually I need to create this as a stored procedure SO here is what i haveCREATE Function FinalAmount(@DepositType char(6))RETURNS realASBEGINDECLARE @FinalAmount(real)IF @DepositType = 'saving' set @FinalAmount = (NumberOfYears * PrincipleAmount * [dbo].[IntRate](NumberofYears)AS rate)ELSE set @FinalAmount = PrincipleAmount * (1 + [ dbo].[IntRate](NumberofYears)AS rate) * NumberOfYearsRETURN @FinalAmountENDThese are the errors I am gettingMsg 102, Level 15, State 1, Procedure FinalAmount, Line 5Incorrect syntax near '('.Msg 156, Level 15, State 1, Procedure FinalAmount, Line 6Incorrect syntax near the keyword 'AS'.Msg 156, Level 15, State 1, Procedure FinalAmount, Line 7Incorrect syntax near the keyword 'AS'.Msg 137, Level 15, State 2, Procedure FinalAmount, Line 8Must declare the scalar variable "@FinalAmount". |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2010-03-29 : 12:02:57
|
What is [dbo].[IntRate](NumberofYears)?CREATE Function FinalAmount(@DepositType char(6))RETURNS realASBEGINDECLARE @FinalAmount realSelect @FinalAmount =case when @DepositType = 'saving' then (NumberOfYears * PrincipleAmount * [dbo].[IntRate](NumberofYears))else PrincipleAmount * (1 + [ dbo].[IntRate](NumberofYears)) * NumberOfYears endfrom CustomersRETURN @FinalAmountEND Harsh Athalyehttp://www.letsgeek.net/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-29 : 12:41:58
|
| seems like another scalar valued udf------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
suki_66
Starting Member
12 Posts |
Posted - 2010-03-29 : 12:45:50
|
| That is a function that was created to determine the rate of interest used based on how many years the money has been depositedso using select would work better than an if/else?also when i try to view the results - it does not work usingSELECT CustID, CustName, NumberofYears, dbo.FinalAmount(DepositType) AS FinalAmountFROM CustomersMsg 4121, Level 16, State 1, Line 2Cannot find either column " dbo" or the user-defined function or aggregate " dbo.IntRate", or the name is ambiguous.so i am thinking it is formatted incorrectly.....I do have the function IntRate listed in this thread |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-29 : 12:56:16
|
| try dbo.[IntRate] instead------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
suki_66
Starting Member
12 Posts |
Posted - 2010-03-29 : 13:00:18
|
quote: Originally posted by visakh16 try dbo.[IntRate] instead------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
this is the error i get nowMsg 4104, Level 16, State 1, Procedure FinalAmount, Line 8The multi-part identifier "dbo.IntRate" could not be bound.Msg 4104, Level 16, State 1, Procedure FinalAmount, Line 9The multi-part identifier " dbo.IntRate" could not be bound.this is the revised codeCREATE Function FinalAmount(@DepositType char(6))RETURNS realASBEGINDECLARE @FinalAmount realDECLARE @NumberOFYears intDECLARE @PrincipleAmount floatIF @DepositType = 'saving' set @FinalAmount = (@NumberOfYears * @PrincipleAmount * [dbo].[IntRate])ELSE set @FinalAmount = (@PrincipleAmount * (1 + ([ dbo].[IntRate]) * @NumberOfYears))RETURN @FinalAmountEND |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-29 : 13:03:17
|
| have you created the intrate udf already?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
suki_66
Starting Member
12 Posts |
Posted - 2010-03-29 : 13:05:42
|
quote: Originally posted by visakh16 have you created the intrate udf already?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
yes i have already created it.....and it is a scalar UDFhere is the intRate codeCREATE FUNCTION [dbo].[IntRate](@NumberofYears decimal(7,4)) RETURNS decimal(7,4)ASBEGIN DECLARE @rate decimal(7,4) if @NumberofYears>=20 set @rate =.15 else if @NumberofYears >= 11 set @rate =.125 else if @NumberofYears>= 6 set @rate =.10 else if @NumberofYears>= 4 set @rate =.075 else if @NumberofYears>= 1 set @rate =.05 else set @rate = 0 RETURN @rateEND |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-29 : 13:08:49
|
tryCREATE Function FinalAmount(@DepositType char(6))RETURNS realASBEGINDECLARE @FinalAmount realDECLARE @NumberOFYears intDECLARE @PrincipleAmount floatIF @DepositType = 'saving' select @FinalAmount = (@NumberOfYears * @PrincipleAmount * [dbo].[IntRate])ELSE select @FinalAmount = (@PrincipleAmount * (1 + ([dbo].[IntRate]) * @NumberOfYears))RETURN @FinalAmountEND ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
suki_66
Starting Member
12 Posts |
Posted - 2010-03-30 : 00:02:37
|
| I finally figured it out - thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-30 : 14:27:51
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|