sql server 2008中的apply运算符使用方法

所属分类: 数据库 / mssql2008 阅读数: 200
收藏 0 赞 0 分享

Apply运算符可以实现两个查询结果的全组合结果,又称为交叉集合。例如两个数据组合(A,B)、(A,B),他们的交叉集合为(AA,AB,AA,AB)。

Apply分为Cross Apply和Outer Apply两种使用方式。具体分析如下:

首先先建立两个表StudentList和ScoreInfo。脚本语言如下:

复制代码 代码如下:

create table StudentList(
id int Identity(1,1) not null,
Name nvarchar(20) not null,
Sex bit not null,
Birthday date not null,
Class nvarchar(2) not null,
Grade nvarchar(2) not null,
regdate date not null,
Primary key (id));

create table ScoreInfo(
id int Identity(1,1) not null primary key,
StudentID int not null,
ClassID int not null,
Score int not null,
TestDate date not null,
regdate date not null);


其中ScoreInfo中的StudentID为StudentList中id的外键

插入数据,脚本如下

复制代码 代码如下:

insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate) values('张三', 1, '1988-05-28', 1, 8, '2010-05-05');

insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate) values('李四', 1, '1985-09-13', 4, 4, '2010-05-05');

insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate) values('王丽', 0, '1987-11-05', 1, 7, '2010-05-05');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(1, 1, 98, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(1, 2, 92, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(1, 3, 86, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(2, 1, 95, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(2, 2, 94, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(2, 3, 91, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(3, 1, 90, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(3, 2, 88, '2010-04-15', '2010-05-01');

insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(3, 3, 90, '2010-04-15', '2010-05-01');


两个表结构建立完毕,数据也成功插入进去了。为了便于讲解在StudentList表中再插入一条记录
复制代码 代码如下:

insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate)
values('李铭', 1, '1989-05-04', 2, 7, '2010-05-05');

输入以下语句
复制代码 代码如下:

select * from StudentList a
cross apply
(select ClassID, Score from ScoreInfo where StudentID=a.id) b;

结果如下

再输入以下语句

select * from StudentList a
outer apply
(select ClassID, Score from ScoreInfo where StudentID=a.id) b;

结果如下

可以看出Cross Apply和Outer Apply的区别

Cross Apply把语句两边的两个Select查询结果进行交叉配对,将所有结果展示出来。Cross Apply查询确保在查询两个子集数据的交集时,只有有效信息的集合才被列出来。

OuterApply查询是把两个子集的所有组合列了出来,不管数据是否有交叉,全部显示要配对的数据。

更多精彩内容其他人还在看

SQL Server 2008登录错误:无法连接到(local)解决方法

在一些朋友安装完SQL Server 2008之后大多会遇到连接出错的问题比如:SQL Server 2008登录错误:无法连接到(local)等等相关问题,本文将详细介绍解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL2008 附加数据库提示5120错误解决方法

有些朋友在操作sql2008数据库时会提示5120错误,本文将介绍详细的解决方法,有需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server储过程加密和解密原理深入分析

在网络上,看到有SQL Server 2000和SQL Server 2005 的存储过程加密和解密的方法,后来分析了其中的代码,发现它们的原理都是一样的;感觉这两个存储过程蛮有意思的,拿来与大家分享,需要了解的朋友可以参考下
收藏 0 赞 0 分享

Excel导入数据库时出现的文本截断问题解决方案

在把Excel导入到数据库中时,发生文本截断问题:即导入的数据每行只有一部分,遇到这样的问题,甚是尴尬,接下来介绍此问题的解决方法,感兴趣的朋友可以了解下,希望本文对你有所帮助
收藏 0 赞 0 分享

数据库日常维护常用的脚本概述及部分测试

今天花点时间在SQL SERVER 2008中运行试试常用的数据库维护脚本,结果发现以前从书本上见过的backup语句和dump语句已经在2008里面消失了,感兴趣的你可不要错过了哈,经验之谈啊,希望可以帮助到你
收藏 0 赞 0 分享

sql2008安装后服务器改名造成名称不一致解决方法

查看服务器名是否一致如果不一致,则以serverproperty(servername)为准,接下来介绍详细解决方法,感兴趣的朋友可以参考下,希望可以帮助到你
收藏 0 赞 0 分享

sql2008评估期已过的解决办法及序列号

sql2008评估期已过的解决办法及序列号,有关如何升级您的测试版软件的信息
收藏 0 赞 0 分享

使用Sqlserver事务发布实现数据同步(sql2008)

事务的功能在sqlserver中由来已久,因为最近在做一个数据同步方案,所以有机会再次研究一下它以及快照等,发现还是有很多不错的功能和改进的。这里以sqlserver2008的事务发布功能为例,对发布订阅的方式简要介绍一下操作流程,一方面做个总结备份,一方面与大家进行一下分享和交
收藏 0 赞 0 分享

SQL2008 附加数据库提示 5120错误 解决办法

SQL2008 附加数据库提示 5120错误 解决办法,需要的朋友可以参考一下
收藏 0 赞 0 分享

如何把Excel数据导入到SQL2008数据库的实例方法

最近想练习一下批量插入数据,所以从网上找了一下资料,做了一个怎么把Excel文件数据导入到数据库。
收藏 0 赞 0 分享
查看更多