SQL Server CROSS APPLY和OUTER APPLY的应用详解
apply有两种形式,一个是outer apply,一个是cross apply,区别在于指定outer,意味着结果集中将包含使右表表达式为空的左表表达式中的行,而指定cross,则相反,结果集中不包含使右表表达式为空的左表表达式中的行。
理解cross apply
比如:largetable表中的某一列存储的数据是以“:”号分隔的数据,我们处理的时候,可能要先把这个值,先分隔,然后把分隔后的每个值单独一行放在一张表中,然后对这个表做处理。这只是用其中一行做的处理,如果我们把表中多行都做这样的处理,把多行以:号分隔的数值都放在一个表中,该怎么处理呢?
用apply表运算符一行语句就能处理以上操作。
select
a
from
dbo.largetable as lt --实际表
cross apply
dbo.split(lt.name,':') --自定义表值函数,完成字符串分隔
where
a <> '' --去掉结果表中a字段为空的数据
-----------字符串分隔函数-------------
create function [dbo].[split]
(
@sql varchar(8000),
@splits varchar(10)
)
returns @temp table (a varchar(100))
as
begin
declare @i int
set @sql = rtrim(ltrim(@sql))
set @i = charindex(@splits,@sql)
while @i >= 1
begin
insert @temp values(left(@sql,@i-1))
set @sql = substring(@sql,@i+1,len(@sql)-@i)
set @i = charindex(@splits,@sql)
end
if @sql <> ''
insert @temp values (@sql)
return
end
------------调用示例-------------
select
*
from
dbo.split('581::579::519::279::406::361::560',':')
apply的执行过程:它先逻辑计算左表表达式,然后把右表达式应用到左表表达式的每一行。实际是把外部查询的列引用作为参数传递给表值函数。
我们知道 sql server2000 中有个 cross join 是用于交叉联接的。实际上sql server 2005 新增 cross apply 和 outer apply 联接语句是用于交叉联接表值函数(返回表结果集的函数)的,更重要的是这个函数的参数是另一个表中的字段。
这个解释可能有些含混不请,请看下面的例子:
-- 1. cross join 联接两个表
select *
from table_1 as t1
cross join table_2 as t2
-- 2. cross join 联接表和表值函数,表值函数的参数是个“常量”
select *
from table_1 t1
cross join fn_tablevalue(100)
-- 3. cross join 联接表和表值函数,表值函数的参数是“表t1中的字段”
select *
from table_1 t1
cross join fn_tablevalue(t1.column_a)
msg 4104, level 16, state 1, line 1
the multi-part identifier "t1.column_a" could not be bound.
最后的这个查询的语法有错误。在cross join 时,表值函数的参数不能是表 t1 的字段, 为啥不能这样做呢?我猜可能微软当时没有加这个功能:),后来有客户抱怨后, 于是微软就增加了 cross apply 和 outer apply 来完善。
请看 cross apply, outer apply 的例子:
-- 4. cross apply
select *
from table_1 t1
cross apply fn_tablevalue(t1.column_a)
-- 5. outer apply
select *
from table_1 t1
outer apply fn_tablevalue(t1.column_a)
cross apply 和 outer apply 对于 t1 中的每一行都和派生表(表值函数根据t1当前行数据生成的动态结果集)做了一个交叉联接。cross apply 和 outer apply 的区别在于:如果根据 t1 的某行数据生成的派生表为空,cross apply 后的结果集 就不包含 t1 中的这行数据,而 outer apply 仍会包含这行数据,并且派生表的所有字段值都为 null。
下面的例子摘自微软 sql server 2005 联机帮助,它很清楚的展现了 cross apply 和 outer apply 的不同之处:
-- cross apply query
select *
from departments as d
cross apply fn_getsubtree(d.deptmgrid) as st
deptid deptname deptmgrid empid empname mgrid lvl
----------- ----------- ----------- ----------- ----------- ----------- ------
1 hr 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 query
select *
from departments as d
outer apply fn_getsubtree(d.deptmgrid) as st
deptid deptname deptmgrid empid empname mgrid lvl
----------- ----------- ----------- ----------- ----------- ----------- ------
1 hr 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 null null null null
(13 row(s) affected)
下面是完整的测试代码,你可以在 sql server 2005 联机帮助上找到:
-- create employees table and insert values
if object_id('employees') 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('departments') is not null
drop table departments
go
-- create departments table and insert values
create table departments
(
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 (2,1,'andrew',00.00)
insert into employees values (3,1,'janet',00.00)
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 (7,3,'robert',00.00)
insert into employees values (8,3,'laura',00.00)
insert into employees values (9,3,'ann',00.00)
insert into employees values (10,4,'ina',00.00)
insert into employees values (11,7,'david',00.00)
insert into employees values (12,7,'ron',00.00)
insert into employees values (13,7,'dan',00.00)
insert into employees values (14,11,'james',00.00)
insert into departments values (1,'hr',2)
insert into departments values (2,'marketing',7)
insert into departments values (3,'finance',8)
insert into departments values (4,'r&d',9)
insert into departments values (5,'training',4)
insert into departments values (6,'gardening',null)
go
--select * from departments
-- 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 departments as d
cross apply fn_getsubtree(d.deptmgrid) as st
-- outer apply query
select *
from departments as d
outer apply fn_getsubtree(d.deptmgrid) as st
推荐阅读
-
SQL Server CROSS APPLY和OUTER APPLY的应用详解
-
SQL联合查询inner join、outer join和cross join的区别详解
-
SQL Server CROSS APPLY和OUTER APPLY的应用详解
-
SQL联合查询inner join、outer join和cross join的区别详解
-
SQL Server CROSS APPLY和OUTER APPLY的应用详解
-
SQL Server CROSS APPLY和OUTER APPLY的应用详解
-
SQL 关于apply的两种形式cross apply 和 outer apply
-
SQL 关于apply的两种形式cross apply 和 outer apply