O'Reilly logo

Inside Microsoft® SQL Server® 2008: T-SQL Querying by Lubor Kollar Itzik Ben-Gan Dejan Sarka, and Steve Kass

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required