Cross apply & outer apply

Source: Internet
Author: User
Tags microsoft sql server 2005 null null

In SQL Server 2005, the cross apply and outer apply join statements are added. What is the role of adding these two statements?

We know that one SQL Server 2000 has a cross join used for cross join. In fact, adding cross apply and outer apply is used to cross join the table value function (the function that returns the table result set). More importantly, the parameter of this function is a field in another table. This explanation may be ambiguous. Please refer to the following example:

-- 1. Cross join two tables
Select *
From table_1 as T1
Cross join table_2 as T2
-- 2. Cross join joins tables and Table value functions. The parameters of the Table value function are constants"
Select *
From table_1 T1
Cross join fn_tablevalue (100)
-- 3. Cross join joins tables and Table value functions. The parameter of the Table value function is "field in Table T1"
Select *
From table_1 T1
Cross join fn_tablevalue (t1.column _)

MSG 4104, level 16, state 1, line 1
The multi-part identifier "t1.column _ A" cocould not be bound.
The last query syntax is incorrect. During cross join, the table value function parameter cannot be a field of table T1. Why cannot this problem be solved? I guess Microsoft didn't add this feature at the time :). After some customers complained about it, Microsoft added cross apply and outer apply to improve it. For details, see cross apply and outer apply:

-- 4. Cross apply
Select *
From table_1 T1
Cross apply fn_tablevalue (t1.column _)

-- 5. Outer apply
Select *
From table_1 T1
Outer apply fn_tablevalue (t1.column _)
Cross apply and outer apply perform a cross join between each row in T1 and the derived table (the table value function generates a dynamic result set based on the data of the current row in T1. The difference between cross apply and outer apply is that if the derived table generated based on a row of T1 is empty, the result set after cross apply does not contain this row of data in T1, however, outer apply still contains this row of data, and all fields in the derived table are null.

The following example is taken from Microsoft SQL Server 2005 online help. It clearly shows the differences between cross apply and outer apply:

-- Cross apply
Select *
From orders as d
Cross apply fn_getsubtree (D. deptmgrid) as St
Deptid deptname deptmgrid empid empname mgrid LVL
------------------------------------------------------------------------
1 hr 2 2 2 Andrew 1 0
1 hr 2 5 Steven 2 1
1 hr 2 6 Michael 2 1
2 marketing 7 7 Robert 3 0
2 marketing 7 11 David 7 1
2 marketing 7 12 Ron 7 1
2 marketing 7 13 Dan 7 1
2 marketing 7 14 James 11 2
3 finance 8 8 Laura 3 0
4 R & D 9 9 Ann 3 0
5 training 4 4 Margaret 1 0
5 training 4 10 ina 4 1

(12 row (s) affected)
-- Outer apply
Select *
From orders as d
Outer apply fn_getsubtree (D. deptmgrid) as St
Deptid deptname deptmgrid empid empname mgrid LVL
------------------------------------------------------------------------
1 hr 2 2 2 Andrew 1 0
1 hr 2 5 Steven 2 1
1 hr 2 6 Michael 2 1
2 marketing 7 7 Robert 3 0
2 marketing 7 11 David 7 1
2 marketing 7 12 Ron 7 1
2 marketing 7 13 Dan 7 1
2 marketing 7 14 James 11 2
3 finance 8 8 Laura 3 0
4 R & D 9 9 Ann 3 0
5 training 4 4 Margaret 1 0
5 training 4 10 ina 4 1
6 gardening null

(13 row (s) affected)
Note the last row in the outer apply result set. When the last row of the departments performs a cross join: deptmgrid is null, fn_getsubtree (D. the derived table generated by deptmgrid does not have data, but outer apply still contains this row of data, which is different from cross join.

The following is the complete test code, which can be found on SQL Server 2005 online help:

-- Create employees table and insert values
If object_id ('ployees') is not null
Drop table employees
Go
Create Table employees
(
Empid int not null,
Mgrid int null,
Empname varchar (25) not null,
Salary money not null
)
Go
If object_id ('conditions') is not null
Drop table orders ments
Go
-- Create orders ments table and insert values
Create Table orders ments
(
Deptid int not null primary key,
Deptname varchar (25) not null,
Deptmgrid int
)
Go

-- Fill datas
Insert into employees values (1, null, 'nancy, 00.00)
Insert into employees values (00.00, 'Andrew)
Insert into employees values (00.00, 'cid)
Insert into employees values (4, 1, 'Margaret, 00.00)
Insert into employees values (5, 2, 'steven, 00.00)
Insert into employees values (6, 2, 'Michael ', 00.00)
Insert into employees values (00.00, 'Robert)
Insert into employees values (8, 3, 'laur', 00.00)
Insert into employees values (9, 3, 'ann ', 00.00)
Insert into employees values (00.00, 'ina)
Insert into employees values (11,7, 'David', 00.00)
Insert into employees values (00.00, 'ron)
Insert into employees values (00.00, 'dan)
Insert into employees values (14,11, 'James ', 00.00)

Insert into orders ments values (1, 'hr', 2)
Insert into attributes values (2, 'marketing ', 7)
Insert into orders ments values (3, 'Finance ', 8)
Insert into orders ments values (4, 'r & D', 9)
Insert into attributes values (5, 'Training', 4)
Insert into attributes values (6, 'gardening', null)
Go
-- Select * from orders ments

-- Table-value function
If object_id ('fn _ getsubtree ') is not null
Drop function fn_getsubtree
Go
Create Function DBO. fn_getsubtree (@ empid as INT)
Returns table
As
Return (
With employees_subtree (empid, empname, mgrid, LVL)
As
(
-- Anchor member (AM)
Select empid, empname, mgrid, 0
From employees
Where empid = @ empid
Union all
-- Recursive member (RM)
Select E. empid, E. empname, E. mgrid, es. LVL + 1
From employees as E
Join employees_subtree as es
On E. mgrid = es. empid
)
Select * From employees_subtree
)
Go

-- Cross apply Query
Select *
From orders as d
Cross apply fn_getsubtree (D. deptmgrid) as St

 

-- Outer apply Query
Select *
From orders as d
Outer apply fn_getsubtree (D. deptmgrid) as St

 

----------------- Two small examples of Chinese style arrangement -------------------------------
Create Table # T (name varchar (10 ))
Insert into # T values ('zhang san ')
Insert into # T values ('Li si ')
Insert into # T values (null)

Create Table # T2 (name varchar (10), course varchar (10), score INT)
Insert into # T2 values ('zhang san', 'China', 74)
Insert into # T2 values ('zhang san', 'mat', 83)
Insert into # T2 values ('zhang san', 'Physical ', 93)
Insert into # T2 values (null, 'mat', 50)

-- Drop table # T, # T2
Go

Select
*
From
# T
Cross apply
(Select course, score from # T2 where name = A. Name) B

/*
Name course score
-------------------------------
Zhang San Language 74
James math 83
Zhang San physical 93

(3 rows affected)

*/

Select
*
From
# T
Outer apply
(Select course, score from # T2 where name = A. Name) B
/*
Name course score
-------------------------------
Zhang San Language 74
James math 83
Zhang San physical 93
Li Si null
Null null

(5 rows affected)

*/

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/htl258/archive/2009/09/10/4537421.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.