Functions Flashcards
What are the 2 types of user-defined functions (UDFs) that SQL Server supports?
Scalar and Table-valued
Can functions update or change a database?
No
What is a scalar function?
Returns a single value
What does inlined mean?
The compiler will automatically incorporate the whole body of the function into the surrounding code
What is interpreted code?
compiler invoking it from the main body of code requires a jump to a different code block to execute the function.
What is the biggest potential drawback of SQL Server Scalar functions?
SQL Server will execute the function once for every row in the result set
How many times are Table Valued Functions called?
SQL Server will call them only once, regardless of the number of rows in the result set
What is deterministic?
Given the same inputs will return the same result each time.
Give an example of a non-deterministic function?
GetDate()
How many times are non-deterministic functions called?
Once per statement
How many times are deterministic functions called
Once per row
Why use schema binding?
Makes it more difficult to make changes to the underlying data structures that would break our functions
How can you prevent function processing data when a null is provided for an input
Use the RETURNS NULL ON NULL INPUT option, which will cause SQL Server to immediately return NULL
How can you tell if a scalar function is being called once for every row in dataset
Look at the query execution plan. if in the properties it references the function name (rather than the column name), then the function is being called once per row.
Are there performance implication to using a scalar function in the WHERE clause?
Yes, If you are using an indexed column within the scalar function then this can lead to a full table scan rather than use of the index