all groups > sql server programming > november 2006 >
You're in the

sql server programming

group:

When do functions get applied?


When do functions get applied? Matt
11/15/2006 9:44:01 PM
sql server programming: Given a statement such as this:

SELECT myUDF(ColumnA), ColumnB
FROM tableQ
WHERE ColumnB = 'SomeValue'

Does the myUDF function still get executed against every record in the
table, even though the WHERE clause contains enough information to allow the
myUDF function to execute against only the filtered subset of records that
would be returned?

I could understand that a slightly modified version of it...

SELECT ColumnA, ColumnB
FROM tableQ
WHERE myUDF(ColumnB) = 'SomeTransformedValue'

....would actually require that the function is executed against every record
to evaluate the condition. But what would the behavior be on the former
example?

To beat the dead horse, I would expect SQL to be smart enough to only
execute that function once if given the following syntax:

SELECT TOP 1 myUDF(ColumnA)
FROM tableQ


The answer will help me determine whether I want to take a user defined
function approach to manipulating some values or if I'll push this
manupulation back to my business layer...

Thanks in advance!
Re: When do functions get applied? Matt
11/15/2006 10:15:02 PM
A UDF that converts the stored UTC datetime to the localtime of a specific
time zone. It returns a single nvarchar.

[quoted text, click to view]
Re: When do functions get applied? Matt
11/15/2006 11:37:01 PM
I did read the article, yes. I am aware of the potential for performance
hits. Thank you for your reply.

[quoted text, click to view]
Re: When do functions get applied? Uri Dimant
11/16/2006 7:56:22 AM
Matt
What kind of UDF do you have ?
http://www.sql-server-performance.com/user_defined_functions.asp





[quoted text, click to view]

Re: When do functions get applied? Uri Dimant
11/16/2006 8:32:40 AM
Matt
I hope you read the article I have posted here

SELECT myUDF(ColumnA), ColumnB
FROM tableQ
WHERE ColumnB = 'SomeValue'

In that case an UDF will be executed for each rows in the table that
matching to the WHERE condition




[quoted text, click to view]

Re: When do functions get applied? Tibor Karaszi
11/16/2006 8:43:48 AM
[quoted text, click to view]

It will be evaluated for only the rows that matches your condition.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]

AddThis Social Bookmark Button