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.

We get back the same set as with the stand-alone query. Even for a simple query like this one, the new<br />

code is quite a bit more readable. The call works pretty much as it would from most languages that support<br />

functions. There is, however, one hitch — the schema is required. <strong>SQL</strong> <strong>Server</strong> will, for some reason,<br />

not resolve scalar value functions the way it does with other objects.<br />

As you might expect, there is a lot more to UDFs than just readability. You can embed queries in them<br />

and use them as an encapsulation method for subqueries. Almost anything you can do procedurally that<br />

returns a discrete value could also be encapsulated in a UDF and used inline with your queries.<br />

Let’s take a look at a very simple subquery example. The subquery version looks like this:<br />

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

SELECT Name,<br />

ListPrice,<br />

(SELECT AVG(ListPrice) FROM Production.Product) AS Average,<br />

ListPrice - (SELECT AVG(ListPrice) FROM Production.Product)<br />

AS Difference<br />

FROM Production.Product<br />

WHERE ProductSubCategoryID = 1; -- The Mountain Bikes Sub-cat<br />

This gets us back a pretty simple set of data:<br />

Name ListPrice Average Difference<br />

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

Mountain-100 Silver, 38 3399.99 438.6662 2961.3238<br />

Mountain-100 Silver, 42 3399.99 438.6662 2961.3238<br />

Mountain-100 Silver, 44 3399.99 438.6662 2961.3238<br />

Mountain-100 Silver, 48 3399.99 438.6662 2961.3238<br />

Mountain-100 Black, 38 3374.99 438.6662 2936.3238<br />

Mountain-100 Black, 42 3374.99 438.6662 2936.3238<br />

…<br />

…<br />

Mountain-500 Silver, 52 564.99 438.6662 126.3238<br />

Mountain-500 Black, 40 539.99 438.6662 101.3238<br />

Mountain-500 Black, 42 539.99 438.6662 101.3238<br />

Mountain-500 Black, 44 539.99 438.6662 101.3238<br />

Mountain-500 Black, 48 539.99 438.6662 101.3238<br />

Mountain-500 Black, 52 539.99 438.6662 101.3238<br />

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

Let’s try it again, only this time we’ll encapsulate both the average and the difference into two functions.<br />

The first encapsulates the task of calculating the average and the second does the subtraction.<br />

CREATE FUNCTION dbo.AveragePrice()<br />

RETURNS money<br />

WITH SCHEMABINDING<br />

AS<br />

BEGIN<br />

RETURN (SELECT AVG(ListPrice) FROM Production.Product);<br />

END<br />

GO<br />

Chapter 13: User-Defined Functions<br />

415

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

Saved successfully!

Ooh no, something went wrong!