Solved

Error in SQL Function

Posted on 2009-07-06
3
202 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now