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

所属分类: 数据库 / MsSql 阅读数: 1251
收藏 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 分组查询问题

sql 分组查询问题,需要的朋友可以参考下。
收藏 0 赞 0 分享

MSSQL 数据库备份和还原的几种方法 图文教程

MSSQL 数据库备份和还原的几种方法小结,配有图文,大家看了就知道了。
收藏 0 赞 0 分享

GridView自定义分页的四种存储过程

首先要说说为什么不用GridView的默认的分页功能,GridView控件并非真正知道如何获得一个新页面,它只是请求绑定的数据源控件返回适合规定页面的行,分页最终是由数据源控件完成。
收藏 0 赞 0 分享

Godaddy 导入导出MSSQL数据库的实现步骤

可以从限制文件中导入SQL共享服务器数据库。如果想把存放在其他地方的数据导入,需要先把其内容拷到限制文件中。(
收藏 0 赞 0 分享

得到自增列的下一个会插入的id

怎么得到自增列的下一个会插入的id
收藏 0 赞 0 分享

几个简单的基本的sql语句

这个是从网上看到的。。。COPY一下。。有时候忘记了。。
收藏 0 赞 0 分享

sqlserver 自动备份所有数据库的SQL

可自动备份除系统数据库外的所有数据库。备份文件的周期保存周期可以更改。
收藏 0 赞 0 分享

SQL Server 服务器优化技巧浅谈

数据文件和日志文件的操作会产生大量的I/O。在可能的条件下,日志文件应该存放在一个与数据和索引所在的数据文件不同的硬盘上以分散I/O,同时还有利于数据库的灾难恢复。
收藏 0 赞 0 分享

sql2005 存储过程分页示例代码

sql2005分页存储过程示例
收藏 0 赞 0 分享

union组合结果集时的order问题

如果能确定各查询结果不会有重复的项,最好就带上all,因为这样还是可以提高一些效率的。
收藏 0 赞 0 分享
查看更多