Submitted byCategory
Review Cycle
.
Public
Joachim Mutter/sysarc
on 03/11/2009 at 11:13 AM
SQL\Code, SQLServer\Documentation

Apply Operator - Corrolated Join

The base info about this article comes from SQLServer-Central and was written by Gregory Jackson
(Deutsche Übersetzung)

The consens of that article is:

if you want to use restrictive values in a JOIN operation with a subselect or a UDF from an outer expression, you will run into an error like "identifier not be bound". the logical description for such an operation would be "Correlated Join" the subselect corrolates to informations from the outer (left in MS Help terms) expressen.


This statement would be the first Try and results in an error

=> Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "D.deptmdrid" could not be bound.


Whereas this statement works without errors and retiurns the expected results



Remark:
Instead of an UDF you could also use a Subselect as right Part of the APPLY operator ...


Microsoft Help:

The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function acts as the right input and the outer table expression acts as the left input. The right input is evaluated for each row from the left input and the rows produced are combined for the final output. The list of columns produced by the APPLY operator is the set of columns in the left input followed by the list of columns returned by the right input.

There are two forms of APPLY: CROSS APPLY and OUTER APPLY. CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function. OUTER APPLY returns both rows that produce a result set, and rows that do not, with NULL values in the columns produced by the table-valued function.


left_table_source { CROSS | OUTER } APPLY right_table_source