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.

Chapter 13: User-Defined Functions<br />

418<br />

To simplify things a bit, we’ll encapsulate everything in a function instead:<br />

USE AdventureWorks<strong>2008</strong>;<br />

GO<br />

CREATE FUNCTION dbo.fnContactSearch(@LastName nvarchar(50))<br />

RETURNS TABLE<br />

AS<br />

RETURN (SELECT p.BusinessEntityID,<br />

LastName + ‘, ‘ + FirstName AS Name,<br />

ea.EmailAddress<br />

FROM Person.Person as p<br />

LEFT OUTER JOIN Person.EmailAddress ea<br />

ON ea.BusinessEntityID = p.BusinessEntityID<br />

WHERE LastName Like @LastName + ‘%’);<br />

GO<br />

Now we’re set up pretty well — to execute it, we just call the function and provide the parameter:<br />

SELECT *<br />

FROM fnContactSearch(‘Ad’);<br />

And we get back the same result set — no WHERE clause, no filtering the SELECT list, and, as our friends<br />

down under would say, no worries; we can use this over and over again without having to use the old<br />

cut-and-paste trick. Note, also, that while you could have achieved similar results with a sproc and an<br />

EXEC command, you couldn’t directly join the results of the sproc to another table.<br />

Well, all this would probably be exciting enough, but sometimes we need more than just a single SELECT<br />

statement. Sometimes, we want more than just a parameterized view. Indeed, much as we saw with some<br />

of our scalar functions, we may need to execute multiple statements in order to achieve the results that<br />

we want. User-defined functions support this notion just fine. Indeed, they can return tables that are created<br />

using multiple statements — the only big difference when using multiple statements is that you<br />

must both name and define the metadata (much as you would for a temporary table) for what you’ll be<br />

returning.<br />

To illustrate this example, we’ll discuss a very common problem in the relational database world —<br />

hierarchical data.<br />

Imagine for a moment that you are working in the human resources department. You have an Employee<br />

table, and it has a unary relationship (a foreign key that relates to another column in the same table) that<br />

relates employees to their bosses through the ManagerID column — that is, the way you know who is<br />

someone’s boss, is by relating the ManagerID column back to another EmployeeID. A very common need<br />

in a scenario like this is to be able to create a reporting tree — that is, a list of all of the people who exist<br />

below a given manager in an organization chart.<br />

Historically, relational databases had a major weakness in dealing with hierarchical data. Numerous<br />

articles, white papers, and books have been written on this subject. Fortunately for us, <strong>SQL</strong> <strong>Server</strong> <strong>2008</strong><br />

introduces a new methodology for dealing with hierarchical data. The newly introduced features are the<br />

hierarchyID data type and a collection of built-in functions to help deal with tree type data structures

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

Saved successfully!

Ooh no, something went wrong!