Tip of the Day : How to Join with an Inline User-Defined Function

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - December 19, 2018

How to Join with an Inline User-Defined Function

An inline user-defined function is a type of function which returns a table data type instead of just a scalar value and can be used to achieve the functionality of parameterized views.  An inline user-defined function can be used where table or view expressions are allowed in Transact-SQL queries.  The table returned by an inline user-defined function can be referenced in the FROM clause of a Transact-SQL statement.

Performing a JOIN with an inline user-defined function where the value passed as a parameter to the function comes from the table being joined with it, the INNER JOIN (or LEFT OUTER JOIN) cannot be used as it will give an error of

Msg 4104, Level 16, State 1, Line 2
The multi-part identified could not be bound.

To illustrate, given the following tables and inline user-defined function:

CREATE TABLE [dbo].[MLBTeam] (
 [TeamCode]    CHAR(3),
 [TeamName]    VARCHAR(50)
)


CREATE TABLE [dbo].[MLBPitcher] (
 [PitcherName]    VARCHAR(50),
 [TeamCode]       CHAR(3),
 [ERA]            DECIMAL(6, 3)
)


CREATE FUNCTION [dbo].[ufn_GetTopPitchers] ( @TeamCode CHAR(3) )
RETURNS TABLE AS
RETURN (
    SELECT TOP 3 [TeamCode], [PitcherName]
    FROM [dbo].[MLBPitcher]
    WHERE [TeamCode] = @TeamCode
    ORDER BY [ERA] )

The following INNER JOIN statement will generate an error:

SELECT A.[TeamCode], A.[TeamName], B.[PitcherName]
FROM [dbo].[MLBTeam] A INNER JOIN [dbo].[ufn_GetTopPitchers] ( A.[TeamCode] ) B
          ON A.[TeamCode] = B.[TeamCode]


Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "A.TeamCode" could not be bound.

To perform a join between a table and an inline user-defined function where the value passed to the function parameter comes from a column in the table, the CROSS APPLY operator will be used:

SELECT A.[TeamCode], A.[TeamName], B.[PitcherName]
FROM [dbo].[MLBTeam] A CROSS APPLY [dbo].[ufn_GetTopPitchers] ( A.[TeamCode] ) B

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions