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

所属分类: 数据库 / MsSql 阅读数: 1139
收藏 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 分享

sqlserver还原数据库的时候出现提示无法打开备份设备的解决方法(设备出现错误或设备脱)

今天在恢复数据库的时候,因为是异地部分还原,出现提示 无法打开备份设备 E:\自动备份\ufidau8xTmp\UFDATA.BAK 。设备出现错误或设备脱,这里分享一下解决方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

SQL数据库存储过程示例解析

这篇文章主要针对SQL数据库存储过程示例进行解析,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SQL Server 2012 安全概述

这篇文章给你概括介绍了SQL Server 2012里的基本安全概念。你学到了一些常见的数据威胁,探寻了SQL Server背后的设计理念,学习了在整个系列文章看到的一些安全术语,算是一篇比较非公式化的开篇,希望能够勾引起大家对于sql安全的兴趣
收藏 0 赞 0 分享

探讨select in 在postgresql的效率问题

这篇文章主要介绍了探讨select in 在postgresql的效率问题 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server 2012 身份验证(Authentication)

这篇SQL Server安全文章,我们学习了SQL Server里的多个验证选项。Windows集成身份验证是最安全的,但并不是都是可行的,微软多年来已经让SQL Server验证更加安全。
收藏 0 赞 0 分享

SQL性能优化之定位网络性能问题的方法(DEMO)

这篇文章主要介绍了SQL性能优化之定位网络性能问题的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server 2016里的sys.dm_exec_input_buffer的问题

这篇文章主要介绍了SQL Server 2016里的sys.dm_exec_input_buffer的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server删除表及删除表中数据的方法

本文介绍SQL Server中如何删除表,如何删除表中的数据。在删除表数据时有delete和truncate两种方法,delete和truncate有什么区别呢
收藏 0 赞 0 分享

sqlserver 因为选定的用户拥有对象,所以无法除去该用户的解决方法

这篇文章主要介绍了sqlserver 因为选定的用户拥有对象,所以无法除去该用户,因为是附加数据库选择了与源服务器一样的用户导致
收藏 0 赞 0 分享
查看更多