Cross apply意思

"CROSS APPLY" is a SQL keyword used in Microsoft SQL Server and Azure SQL Database to apply a table-valued function to each row of a table. It is similar to the "JOIN" operation, but instead of joining tables based on a key, it applies a function to each row of a table.

Here's the basic syntax of CROSS APPLY:

SELECT ...
FROM table1
CROSS APPLY table_valued_function(column1, column2, ...)

In this syntax, "table1" is the table from which you want to apply the function, and "table_valued_function" is a function that returns a table. The function is applied to each row of "table1", and the result is combined with the rows of "table1" to form the result set of the query.

CROSS APPLY can be used to perform operations such as:

  1. Apply a function to each row of a table and return the result.
  2. Filter the rows returned by the function.
  3. Perform calculations on the rows returned by the function.

Here's an example of how to use CROSS APPLY:

CREATE FUNCTION dbo.MyTableValuedFunction(@x INT, @y INT)
RETURNS TABLE
AS
RETURN (SELECT @x AS X, @y AS Y);

SELECT *
FROM dbo.MyTableValuedFunction(1, 2) AS F;

In this example, the "dbo.MyTableValuedFunction" function is applied to each row of the "dbo.MyTableValuedFunction" table. The result is a table with two columns, "X" and "Y", each containing the value passed to the function.