Course description
In this course you’ll learn about the three kinds of UDFs available in SQL Server and will see several examples of functions and how to use them. We’ll start by looking at scalar functions that return a single value, then will explore the two types of table-valued functions. These latter functions let you return a table data type that acts like an in-memory table. Once you understand how to create functions, you’ll realize that now you have almost too many options for writing code: views, stored procedures, the different kinds of functions, and others. So we’ll discuss how to decide which kind of code module to use in different situations, as well as some potential performance issues with using functions.
Prerequisites
This course assumes that you have a basic familiarity with the concept of relational databases and a basic understanding of what SQL Server is and the high-level tools in it, as well as how to create and manage objects using Management Studio. You should also have a basic understanding of how SQL Server implements security, including its authentication and authorization schemes, and how to assign permissions on securable objects to principals. You should know the fundamentals of Transact-SQL to write queries to retrieve and update data, and how to execute scripts using the query editor in Management Studio.
It will be very helpful, but not absolutely necessary, to have experience with .NET development using Visual Studio 2010 or 2012 for the portions of the course that deal with SQLCLR development. The .NET code included in this course is written in C#, but if you prefer Visual Basic it should be fairly easy to convert the code.
Learning Paths
This course is part of the following LearnNowOnline SuccessPaths™:
SQL Server Developer
Meet the expert
Don Kiely is a featured instructor on many of our SQL Server and Visual Studio courses. He is a nationally recognized author, instructor, and consultant specializing in Microsoft technologies. Don has many years of teaching experience, is the author or co-author of several programming books, and has spoken at many industry conferences and user groups. In addition, Don is a consultant for a variety of companies that develop distributed applications for public and private organizations.
Course outline
Functions
Overview and Scalar Functions (19:54)
- Introduction (00:41)
- User-Defined Functions Types (01:57)
- User-Defined Functions Rules (03:45)
- Scalar Functions (00:48)
- Scalar Function Syntax (02:12)
- WITH Function Options (02:21)
- Demo: Scalar Function (03:19)
- Demo: Using Parameters (04:00)
- Summary (00:46)
Inline Table-Valued Functions (10:44)
- Introduction (00:40)
- Inline Syntax (01:03)
- Demo: Inline Function (01:26)
- Demo: Executing Inline Function (02:18)
- Demo: Inline with Parameters (02:05)
- Demo: Update with Inline (02:34)
- Summary (00:36)
Multi-Statement (15:37)
- Introduction (00:47)
- Multi-Statement Syntax (01:09)
- Limitations (01:46)
- The TABLE Data Type (00:56)
- Demo: Function Structure (02:18)
- Demo: Function Body (02:55)
- Breaking Change in SQL 2012 (02:14)
- Demo: Selecting Function (02:56)
- Summary (00:32)
Views and Stored Procedures (18:33)
- Introduction (00:33)
- Using Scalar Functions (00:25)
- Demo: Using Scalar Functions (02:14)
- Using Table-Valued Functions (00:46)
- Demo: Table-Valued Function (02:44)
- Performance with UDF (00:58)
- Demo: Performance Problem (03:38)
- Demo: Improving Performance (01:29)
- Multi-Statement Problems (01:48)
- When to Use Functions (03:36)
- Summary (00:18)