?
Solved

Calling the same function twice in a select statement

Posted on 2006-04-27
4
Medium Priority
?
361 Views
Last Modified: 2006-11-18
Hi Gang,

I have a function that I need to call twice in a Stored Procedure.  For example,

    Select
        dbo.calcFundValuation(@sFundID),
        dbo.calcFundValuation(@sFundID) * @fExchangeRate
    From
        ...

My question:
- When I execute the stored procedure, is SQL Server smart enough to call the function only once?  I'm worried about the performance hit if the function gets called twice.

Thanks,
DM
0
Comment
Question by:dgmcpher
  • 3
4 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 16555176
Try this

declare @numVal numeric(5,2)

Set @numVal = dbo.calcFundValuation(@sFundID)

    Select
        @numVal,
        @numVal * @fExchangeRate
    From
        ...
0
 
LVL 33

Expert Comment

by:hongjun
ID: 16555181
You need to store the value somewhere first
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1000 total points
ID: 16555191
dgmcpher,
> smart enough to call the function only once
The Modern processor architecture will check for the value in the cache first and if not found in the cache, it will try to execute..So this will be executed once in that Query unless you provide a different value as the function argument
0
 
LVL 33

Expert Comment

by:hongjun
ID: 16555211
Thanks aneeshattingal for the knowledge :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

864 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