In the below xml cross apply used to join two node. when we join in SQL we mention column name of two table but in cross apply we do not mention column name. so on which column join is performing ?
How sql server understand on which column join will be performing ?
DECLARE @x xml =
'<Orders>
<Order>
<OrderID>13000</OrderID>
<CustomerID>ALFKI</CustomerID>
<OrderDate>2006-09-20Z</OrderDate>
<EmployeeID>2</EmployeeID>
<OrderDetails>
<ProductID>76</ProductID>
<Price>123</Price>
<Qty>10</Qty>
</OrderDetails>
<OrderDetails>
<ProductID>16</ProductID>
<Price>3.23</Price>
<Qty>20</Qty>
</OrderDetails>
</Order>
<Order>
<OrderID>13001</OrderID>
<CustomerID>VINET</CustomerID>
<OrderDate>2006-09-20Z</OrderDate>
<EmployeeID>1</EmployeeID>
<OrderDetails>
<ProductID>12</ProductID>
<Price>12.23</Price>
<Qty>1</Qty>
</OrderDetails>
</Order>
</Orders>'
SELECT OrderID = O.n.value('(OrderID/text())[1]', 'int'),
CustomerID = O.n.value('(CustomerID/text())[1]', 'VARCHAR(MAX)'),
ProductID = D.n.value('(ProductID/text())[1]', 'int'),
Price = D.n.value('(Price/text())[1]', 'decimal(10,2)'),
Qty = D.n.value('(Qty/text())[1]', 'int')
FROM @x.nodes('/Orders/Order') AS O(n)
CROSS APPLY O.n.nodes('OrderDetails') AS D(n)
Here i am giving small example of cross apply where
SELECT * FROM Department D
CROSS APPLY
(
SELECT * FROM Employee E
WHERE E.DepartmentID = D.DepartmentID
) A
see above cross apply here i am mentioning column name on which join will be performing but when join two xml node using cross apply then no column name not mention from two node.....but sql is working....how ?
please help me to understand the situation. thanks