From the course: Complete Guide to Advanced SQL Server

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Table-valued user-defined functions

Table-valued user-defined functions - SQL Server Tutorial

From the course: Complete Guide to Advanced SQL Server

Table-valued user-defined functions

Unlike a scalar-valued function that only returns a single value, a table-valued function returns an entire table, or more specifically, a result set that's made up of columns and rows. In this regard, table-valued functions can act just like a view. But here's the difference, a table-valued function can include input parameters that modify the output. Let's take a look at some order data from the Wide World Importers database. I'm going to run the three SELECT statements on Line 6, 7, and 8 to view some order details. This shows me that we have a sales order for CustomerID Number 803, and taking a look in the Sales.Customers table, I can see who that corresponds to. It's this person here, Bala Dixit. I can also see that the OrderID Number 2 included two line items, and I can see them both right here, LineID Number 3 and LineID Number 6 for a joke mug and a USB flash drive. So that's the structure of the data that we're going to work with. Now, I'd like to write a table-valued…

Contents