Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Redundant Function Calls...

Author  Topic 

smithygreg
Starting Member

37 Posts

Posted - 2008-10-15 : 14:05:24
Hey All..
I have a query that calls a specific function 3 times on the same parameters. Is this bad? Is there a better way to do this..?
Here is basically what I am doing...

select rate*dbo.GetTotalValue(othervalue)
from datatable
join othertable on
dbo.gettotalvalue(othervalue) >= minimum
and
dbo.gettotalvalue(othervalue) < maximum


Is there any way to just make the call for dbo.gettotalvalue() once? Is SQL Server smart enough to not make the call twice? Will it make any difference?

Thanks a whole bunch!
Greg

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-15 : 14:08:40
You can make the call once by placing the value into a variable and then using the variable in your query.

SQL is not smart enough to run it only once as the output could change in between calls for the same input parameter.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -