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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13: User-Defined Functions<br />

Even using a table valued output parameter, you still need to make at least one additional step before<br />

using the results in a query.<br />

With a UDF, however, you can pass parameters in, but not out. Instead, the concept of output parameters<br />

has been replaced with a much more robust return value. As with system functions, you can return a scalar<br />

value — what’s particularly nice, however, is that this value is not limited to just the integer data type<br />

as it would be for a sproc. Instead, you can return most <strong>SQL</strong> <strong>Server</strong> data types (more on this in the next<br />

section).<br />

As they like to say in late-night television commercials: “But wait! There’s more!” The “more” is that you<br />

are actually not just limited to returning scalar values — you can also return tables. This is wildly powerful,<br />

and we’ll look into this fully later in the chapter.<br />

So, to summarize, we have two types of UDFs:<br />

❑ Those that return a scalar value<br />

❑ Those that return a table<br />

Let’s take a look at the general syntax for creating a UDF:<br />

CREATE FUNCTION [.]<br />

( [ [AS] [.] [ = <br />

[READONLY]]<br />

[ ,...n ] ] )<br />

RETURNS {|TABLE [()]}<br />

[ WITH [ENCRYPTION]|[SCHEMABINDING]|<br />

[ RETURNS NULL ON NULL INPUT | CALLED ON NULL INPUT ] | [EXECUTE AS {<br />

CALLER|SELF|OWNER|} ]<br />

]<br />

[AS] { EXTERNAL NAME |<br />

BEGIN<br />

[]<br />

{RETURN |RETURN ()}<br />

END }[;]<br />

This is kind of a tough one to explain because parts of the optional syntax are dependent on the choices<br />

you make elsewhere in your CREATE statement. The big issues here are whether you are returning a<br />

scalar data type or a table and whether you’re doing a T-<strong>SQL</strong>-based function or doing something utilizing<br />

the CLR and .NET. Let’s look at each type individually.<br />

UDFs Retur ning a Scalar V alue<br />

412<br />

This type of UDF is probably the most like what you might expect a function to be. Much like most of<br />

<strong>SQL</strong> <strong>Server</strong>’s own built-in functions, they will return a scalar value to the calling script or procedure;<br />

functions such as GETDATE() or USER() return scalar values.<br />

As I indicated earlier, one of the truly great things about a UDF is that you are not limited to an integer<br />

for a return value — instead, it can be of any valid <strong>SQL</strong> <strong>Server</strong> data type (including user-defined data

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

Saved successfully!

Ooh no, something went wrong!