Lesson 3 of 3 Views and User-Defined Functions

Inline and Multi-Statement Table-Valued Functions

This lesson compares inline and multi-statement table-valued functions and explains how CROSS APPLY and OUTER APPLY call them per row.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish between inline and multi-statement table-valued functions.
  • Explain how interleaved execution affects multi-statement functions.
  • Describe the difference between CROSS APPLY and OUTER APPLY.

Inline table-valued functions

An inline TVF has no body: RETURNS TABLE AS RETURN (SELECT ...). The optimizer expands it into the calling query like a view with parameters, so it uses statistics, indexes and parallelism normally. Inline TVFs are the preferred form for reusable parameterized queries and are required for Row-Level Security predicates.

CREATE OR ALTER FUNCTION Sales.fnRecentOrders (@CustomerID int, @Top int)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
    SELECT TOP (@Top) o.OrderID, o.OrderDate, o.TotalDue
    FROM Sales.Orders AS o
    WHERE o.CustomerID = @CustomerID
    ORDER BY o.OrderDate DESC;
Select all that apply

Inline TVFs

Exam skills 1.2.3

Identify the true statements about inline table-valued functions.

Log in to try this activity Interactive activities are free with an account.

Multi-statement table-valued functions

A multi-statement TVF declares a table variable in its RETURNS clause and fills it with any number of statements, which allows procedural logic such as loops or several passes. The optimizer historically assumed a fixed small row count for its result, which led to poor plans; interleaved execution (compatibility level 140 and later) pauses optimization, runs the function and uses the actual row count for the rest of the plan. The following comparison summarizes the difference.

Inline TVF
BodyA single RETURN (SELECT ...)
OptimizationExpanded into the calling query
PerformanceUsually as fast as the equivalent query
Choose when Almost always
Multi-statement TVF
BodySeveral statements that fill a table variable
OptimizationExecuted separately; row estimate improved by interleaved execution
PerformanceOften slower for large results
Choose when Logic that cannot be expressed as one query
Sort into groups

Inline or multi-statement?

Exam skills 1.2.3

Decide which kind of table-valued function each statement describes.

Log in to try this activity Interactive activities are free with an account.

Using TVFs with APPLY

A TVF in the FROM clause can take column values from a preceding table through CROSS APPLY, which returns only outer rows with at least one function row, or OUTER APPLY, which keeps outer rows with NULLs when the function returns nothing. This expresses per-row queries such as the latest three orders per customer.

SELECT c.CustomerID, c.Name, r.OrderID, r.OrderDate
FROM Sales.Customer AS c
OUTER APPLY Sales.fnRecentOrders(c.CustomerID, 3) AS r;
Worked example

Replacing a multi-statement TVF

A multi-statement TVF returns a customer's open orders with a computed balance in one SELECT inserted into its return table. Queries that join it to large tables choose nested loops and run slowly because the estimates are wrong. The logic is a single query. What should the developer do?

Rewrite it as an inline TVF with the same parameters. Because the logic is a single SELECT, nothing requires the multi-statement form, and the inline version is expanded into the calling query with real statistics. Adding OPTION (RECOMPILE) or relying on interleaved execution improves estimates but still materializes the result in a table variable.

Check yourself

Which APPLY variant keeps outer rows for which the function returns no rows?

Show answer

OUTER APPLY, which returns NULLs for the function's columns; CROSS APPLY removes those rows.

Exam Tips

Remember for the exam

  • Reusable parameterized query: inline TVF (RETURNS TABLE AS RETURN SELECT).
  • Per-row function calls: CROSS APPLY drops rows with no result, OUTER APPLY keeps them.
  • Multi-statement TVFs only when logic cannot be one query.
  • RLS predicates must be schema-bound inline TVFs.