Home arrow SQL Server arrow Page 4 - Creating User Defined Functions In SQL Server 2000
SQL SERVER

Creating User Defined Functions In SQL Server 2000


Microsoft SQL Server 2000 allows us to create our own functions and call them through code. These functions are known as UDF's (User Defined Functions). In this article Mitchell's going to teach us how to create three different types of UDF's and how to work with them through batches of SQL code.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 143
January 24, 2002
TABLE OF CONTENTS:
  1. · Creating User Defined Functions In SQL Server 2000
  2. · What is a user-defined function?
  3. · Creating a scalar UDF
  4. · Creating an inline-table valued UDF
  5. · Creating a multi-statement table valued UDF
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Creating User Defined Functions In SQL Server 2000 - Creating an inline-table valued UDF
(Page 4 of 6 )

The second form of UDF that we're going to create is the inline-table valued UDF. An inline-table valued UDF allows us to use a single select query in the body of our UDF to return a set of rows from any specific table.

Here's the simplified signature of the "CREATE FUNCTION" statement for inline-table valued functions:

CREATE FUNCTION [ owner_name. ] function_name

(

[ { @parameter_name [ AS ] data_type } [ ,...n ] ]

)

RETURNS TABLE

[ AS ]

RETURN [ ( ] select-stmt [ ) ]


As you can see, the signature of the "CREATE FUNCTION" for an inline-table valued UDF is exactly the same as that for a scalar UDF accept that it returns a "TABLE" variable type and has no body.

[Note] The variable type "TABLE" is new to SQL Server 2000 and is used to store a set of table rows. [End Note]

Instead of containing a function body, inline-table valued UDF's specify a select statement after the RETURN keyword. This select statement is executed and all of its matching rows are returned as the value of the function.

Let's create a simple inline-table valued UDF that will return a list of authors from the "authors" table in the "pubs" database who are from California. The design of the author's table looks like this:

The design of the authors table in the pubs database

Using query analyzer, enter and execute the following code:

USE PUBS

GO

CREATE FUNCTION getAuthorsByState

(

@state CHAR(2)

)

RETURNS TABLE

AS

RETURN

(

SELECT au_fname + ' ' + au_lname AS aName

FROM authors

WHERE state = @state

)


We've just created a new UDF named "getAuthorsByState", which will return a list of names of authors whose state field matches the value passed in through the input parameter, @state.

As you can see, we have used a basic SQL query to concatenate the values of the au_fname and au_lname fields with a space. In the where clause, we tell SQL Server to only return authors whose state matches the value of our @state input parameter.

To test our new inline-table valued UDF, clear the code window in query analyzer and enter and execute the following code:

USE PUBS

GO

SELECT * FROM getAuthorsByState('CA')


As you can see, we're referring to our UDF as if it is a physical table. The result of our SELECT statement looks like this:

Using our inline-table valued UDF is easy

Notice how we've specified the value of the @state input parameter within quotes. If we wanted to, we could modify our UDF to also filter users based on their current contract settings:

USE PUBS

GO

ALTER FUNCTION getAuthorsByState

(

@state CHAR(2),

@contract BIT

)

RETURNS TABLE

AS

RETURN

(

SELECT au_fname + ' ' + au_lname AS aName

FROM authors

WHERE state = @state

AND contract = @contract

)


Notice how I've used the ALTER FUNCTION statement to change the contents of our inline-table valued UDF? UDF's can also be deleted from the database using the DROP FUNCTION statement in exactly the same way that we can use "DROP" to delete a database, a table, or a stored procedure.
blog comments powered by Disqus
SQL SERVER ARTICLES

- Executing SQL Server Stored Procedure from P...
- How to Search for Date and Time Values Using...
- Replication: SQL Server 2000 - Part 2
- Replication: SQL Server 2000 - Part 1
- SQL Sever: Storing Code in Binary or Text Fi...
- Execute SQL on Multiple Tables/Columns - New...
- How to Connect to a SQL Server from Visual F...
- SQL Server Hardware Tuning and Performance M...
- Primary Key on Multiple Tables New RDBMS C...
- Migrating from Sybase to SQL Server
- What's Best for DBAs? GUI or T-SQL Comma...
- How to Perform a SQL Server Performance Audit
- An Introduction To The Bulk Copy Utility
- SQL Server Stored Procedures 101
- Building Your First SQL Server 2000 Database

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials