Given the following definitions:
CREATE TABLE MyTable
(
col1 int,
col2 int,
col3 int,
col4 int
)
CREATE FUNCTION fn_MyFunction
(
@Param1 int,
@Param2 int
)
RETURNS TABLE AS RETURN
(
SELECT @Param1 * 2 AS 'res1', @Param2 * 4 AS 'res2', @Param1 AS 'col3'
)
I am trying to join the function to the table so that I get a calculated value (s) per row. Example: In a bookings table, I need to get the price based on a start date and end date.
Here is an example query:
SELECT tbl.col1
, tbl.col2
, tbl.col3
, fn.res1
, fn.res1
FROM MyTable tbl
CROSS APPLY fn_MyFunction(tbl.col3, tbl.col4) fn
WHERE fn.col3 = tbl.col3
While I think I am getting the correct results in my actual query, how does the CROSS APPLY
get the results in this case? Does it get them row-by-row (effectively)? Or does it work in a similar way to CROSS JOIN
(ie: making a Cartesian Product)?