17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

So, as you can see, we can actually have very complex code build our table results for us, but it’s still a<br />

table that results and, as such, it can be used just like any other table.<br />

Understanding Determinism<br />

Any coverage of UDFs would be incomplete without discussing determinism. If <strong>SQL</strong> <strong>Server</strong> is going to<br />

build an index over something, it has to be able to deterministically define (define with certainty) what<br />

the item being indexed is. Why does this matter to functions? Well, because we can have functions that<br />

feed data to things that will be indexed (computed column or indexed view).<br />

User-defined functions can be either deterministic or non-deterministic. The determinism is not defined<br />

by any kind of parameter, but rather by what the function is doing. If, given a specific set of valid inputs,<br />

the function will return exactly the same value every time, then the function is said to be deterministic.<br />

An example of a built-in function that is deterministic is SUM(). The sum of 3, 5, and 10 is always going<br />

to be 18 — every time the function is called with those values as inputs. The value of GETDATE(), however,<br />

is non-deterministic — it changes pretty much every time you call it.<br />

To be considered deterministic, a function has to meet four criteria:<br />

❑ The function must be schema-bound. This means that any objects that the function depends on<br />

will have a dependency recorded and no changes to those objects will be allowed without first<br />

dropping the dependent function.<br />

❑ All other functions referred to in your function, regardless of whether they are user- or systemdefined,<br />

must also be deterministic.<br />

❑ The function cannot reference tables that are defined outside the function itself. (Use of table variables<br />

is fine. Temporary tables are fine as long they are defined inside the scope of the function.)<br />

❑ The function cannot use an extended stored procedure.<br />

The importance of determinism shows up if you want to build an index on a view or computed column.<br />

Indexes on views or computed columns are only allowed if the result of the view or computed column can<br />

be reliably determined. This means that, if the view or computed column refers to a non-deterministic<br />

function, no index will be allowed on that view or column. This situation isn’t necessarily the end of the<br />

world, but you will want to think about whether a function is deterministic or not before creating<br />

indexes against views or columns that use that function.<br />

So, this should beget the question: “How do I figure out whether my function is deterministic or not?”<br />

Well, beyond checking the rules we’ve already described, you can also have <strong>SQL</strong> <strong>Server</strong> tell you whether<br />

your function is deterministic or not — it’s stored in the IsDeterministic property of the object. To<br />

check this out, you can make use of the OBJECTPROPERTY function. For example, we could check out the<br />

determinism of our DayOnly function that we used earlier in the chapter:<br />

USE Accounting;<br />

SELECT OBJECTPROPERTY(OBJECT_ID(‘DayOnly’), ‘IsDeterministic’);<br />

It may come as a surprise to you (or maybe not) that the response is that this function is not deterministic:<br />

-----------<br />

0<br />

(1 row(s) affected)<br />

Chapter 13: User-Defined Functions<br />

423

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!