Avatar of dgmcpher
dgmcpher

asked on 

Calling the same function twice in a select statement

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
Microsoft SQL Server

Avatar of undefined
Last Comment
hongjun
Avatar of hongjun
hongjun
Flag of Singapore image

Try this

declare @numVal numeric(5,2)

Set @numVal = dbo.calcFundValuation(@sFundID)

    Select
        @numVal,
        @numVal * @fExchangeRate
    From
        ...
Avatar of hongjun
hongjun
Flag of Singapore image

You need to store the value somewhere first
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of hongjun
hongjun
Flag of Singapore image

Thanks aneeshattingal for the knowledge :)
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo