APPLY

The APPLY table operator applies the right-hand table expression to every row of the left-hand table expression. Unlike a join, where the order in which each of the table expressions is evaluated is unimportant, APPLY must logically evaluate the left table expression first. This logical evaluation order of the inputs allows the right table expression to be correlated with the left one. The concept can probably be made clearer with an example.

Run the following code to create an inline table-valued function called GetTopProducts:

IF OBJECT_ID('dbo.GetTopProducts') IS NOT NULL DROP FUNCTION dbo.GetTopProducts; GO CREATE FUNCTION dbo.GetTopProducts (@supid AS INT, @catid INT, @n AS INT) RETURNS TABLE AS RETURN SELECT TOP (@n) WITH TIES productid, ...

Get Inside Microsoft® SQL Server® 2008: T-SQL Querying now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.