If I have a select statement with a scalar function in it used in various calculations, does that scalar function get called multiple times? If it does, is there a way to optimize this so it only calls the funciton once per select, as in my real query it will be called thousands of times, X 6 times per select.
For example:
SELECT
[dbo].[fn_Days](@Account) + u.[DayRate],
[dbo].[fn_Days](@Account) / u.[WorkDays]
FROM [dbo].[tblUnit] u
All fn_days does is return an int of days worked.
Yes the scalar gets called multiple times the way that you have coded it. One way to make it work would be to wrap it into a subquery like this:
This way fn_Days only gets called once per row, rather than twice, or six times like you mentioned.
Hope this helps.