被遗忘的SQLServer比较运算符谓词

所属分类: 数据库 / MsSql 阅读数: 1171
收藏 0 赞 0 分享
官方的参考文档
http://technet.microsoft.com/zh-cn/library/ms187074%28SQL.90%29.aspx
他们作用于比较运算符和子查询之间,作用类似Exists、not exists、in、not in以及其他逻辑意义,这些语法同样被SQLServer2000支持但是很少看到有人用它们。
复制代码 代码如下:

set nocount on
use tempdb
go
if (object_id ('t1' ) is not null ) drop table t1
create table t1 (n int )
insert into t1 select 2 union select 3
if (object_id ('t2' ) is not null ) drop table t2
create table t2 (n int )
insert into t2 select 1 union select 2 union select 3 union select 4
select * from t2 where n> all (select n from t1 ) --4
select * from t2 where n> any (select n from t1 ) --3,4
--select * from t2 where n>some(select n from t1) --3,4
select * from t2 where n= all (select n from t1 ) --无数据
select * from t2 where n= any (select n from t1 ) --2,3
--select * from t2 where n=some(select n from t1) --2,3
select * from t2 where n< all (select n from t1 ) --1
select * from t2 where n< any (select n from t1 ) --1,2
--select * from t2 where n<some(select n from t1) --1,2
select * from t2 where n<> all (select n from t1 ) --1,4
select * from t2 where n<> any (select n from t1 ) --1,2,3,4
--select * from t2 where n<>some(select n from t1)--1,2,3,4
set nocount off

注意,如果t1中包含null数据,那么所有All相关的比较运算将不会返回任何结果,原因就不用多解释了。而因为t1和t2表的null的存在他们和not exists之类的比较符会有一些区别。
比如下面两句
select * from t2 a where not exists(select 1 from t1 where n>=a.n)
select * from t2 where n >all(select n from t1)
他们逻辑上意义很像但是对于null的处理却是恰恰相反,第一句会忽略子查询的null而把t2的null同时查出来,第二句却是忽略了t2的null同时会因为t1中的null而无法查询到数据。
更多精彩内容其他人还在看

浅析SQL Server 聚焦索引对非聚集索引的影响

本篇文章对SQL Server的聚焦索引和非聚集索引进行简单分析,从而总结出聚焦索引对非聚集索引的影响。有兴趣的朋友可以看下
收藏 0 赞 0 分享

浅析SQL Server的聚焦使用索引和查询执行计划

本文通过介绍默认使用索引、强制使用聚集索引、强制使用非聚集索引让我们知道对于检索所有列结果集使用主键的聚集索引是最佳选择。有兴趣的朋友可以看下
收藏 0 赞 0 分享

详解SQL Server中的数据类型

本文主要讲解了SQL中的数据类型以及几个需要注意的地方,简短的内容,深入的理解。有兴趣的朋友可以看下
收藏 0 赞 0 分享

浅析SQL Server的分页方式 ISNULL与COALESCE性能比较

本文上述重点讲述了SQL Server的分页方式,COALESCE和ISNULL函数区别之处,简短的内容,深入的理解。有兴趣的朋友可以看下
收藏 0 赞 0 分享

浅述SQL Server的聚焦强制索引查询条件和Columnstore Index

本文主要讲了强制使用索引条件来进行查询,当对于使用默认创建索引进行查询计划时觉得不是最优解,可以尝试使用强制索引来进行对比找出更好得解决方案。简短的内容,深入的理解.有兴趣的朋友可以看下
收藏 0 赞 0 分享

详解SQL Server的聚焦过滤索引

本文主要讲解了通过过滤索引来提高查询性能,同时也给出了其不同的场景以及其使用优点和明显的缺点。简短的内容,深入的理解,有兴趣的朋友可以看下
收藏 0 赞 0 分享

解析SQL Server聚焦移除(Bookmark Lookup、RID Lookup、Key Lookup)

本文主要讲解索引性能优化,着重对Bookmark Lookup、RID Lookup、Key Lookup三者进行移除的实现进行解析,以此来提高查询性能。希望对大家有所帮助
收藏 0 赞 0 分享

SqlServer 注释符 单行注释与多行注释

这篇文章主要介绍了SqlServer 注释符 单行注释与多行注释,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL设置SQL Server最大连接数及查询语句

今天遇到了关于Sql Server最大连接数(Max Pool Size)的问题,后来通过查找一些资料解决了,所以想着总结下关于SQL Server最大连接数的内容,所以这篇文章主要介绍了SQL设置SQL Server最大连接数与查询语句,有需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

浅谈SQL Server交叉联接 内部联接

本文主要讲了SQL Server的交叉联接和内部联接,同时也给出了使用需要注意的地方。有需要的朋友可以看下
收藏 0 赞 0 分享
查看更多