Solved

Error in SQL Function

Posted on 2009-07-06
3
204 Views
Last Modified: 2012-05-07
Hi Very New to writing functions in SQL Server

I have
alter FUNCTION [dbo].[UtilityExpense](@ClientApplicationID INT, @RecordType varchar(1))
      RETURNS money
AS
BEGIN
    DECLARE @ge MONEY
    DECLARE @we MONEY
    DECLARE @ee MONEY
    DECLARE @TotalUtilities MONEY

    SELECT
      @ge = BudgetGasExpense,
        @we = BudgetWaterExpense,
        @ee = BudgetElectricExpense
    FROM dbo.ClientInformation
    WHERE ClientApplicationID = @ClientApplicationID AND RecordType = @RecordType
   
   
    @TotalUtilities = @ge+@we+@ee  -- error here
   
      RETURN(@TotalUtilities)
END


I get an error  'Incorrect syntax near '@TotalUtilities'

I think it's because the sql statement  needs to be  delimeited. But cant figure out why. I wil be expanding on these calcualtions so I need to get vaues from the table and then performs a series of calculations returning one value

Thanks
chuck
0
Comment
Question by:charlesbaldo
3 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 24785848
alter FUNCTION [dbo].[UtilityExpense](@ClientApplicationID INT, @RecordType varchar(1))
      RETURNS money
AS
BEGIN
    DECLARE @ge MONEY
    DECLARE @we MONEY
    DECLARE @ee MONEY
    DECLARE @TotalUtilities MONEY

    SELECT
      @ge = BudgetGasExpense,
        @we = BudgetWaterExpense,
        @ee = BudgetElectricExpense
    FROM dbo.ClientInformation
    WHERE ClientApplicationID = @ClientApplicationID AND RecordType = @RecordType
   
   
   set  @TotalUtilities = @ge+@we+@ee  -- error here
   
      RETURN(@TotalUtilities)
END


0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24785917
OR you can modify it like this

alter FUNCTION [dbo].[UtilityExpense](@ClientApplicationID INT, @RecordType varchar(1))
      RETURNS money
AS
BEGIN
    DECLARE @ge MONEY
    DECLARE @we MONEY
    DECLARE @ee MONEY
    DECLARE @TotalUtilities MONEY

    SELECT @TotalUtilities =
       ISNULL(BudgetGasExpense,0) +
        ISNULL(BudgetWaterExpense,0)+
         ISNULL(BudgetElectricExpense,0)
    FROM dbo.ClientInformation
    WHERE ClientApplicationID = @ClientApplicationID AND RecordType = @RecordType
   
   
      RETURN(@TotalUtilities)
END
0
 

Author Closing Comment

by:charlesbaldo
ID: 31600196
Thank You
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question