淺析被遺忘的SQLServer比較運算符修飾詞
更新時間:2013年06月26日 17:50:52 作者:
本篇文章是對被遺忘的SQLServer比較運算符修飾詞進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
SQLServer中有三個關(guān)鍵字可以修改比較運算符:All、Any和Some,其中Some和Any等價。
官方的參考文檔
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 ) --無數(shù)據(jù)
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數(shù)據(jù),那么所有All相關(guān)的比較運算將不會返回任何結(jié)果,原因就不用多解釋了。而因為t1和t2表的null的存在他們和not exists之類的比較符會有一些區(qū)別。
比如下面兩句
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而無法查詢到數(shù)據(jù)。
官方的參考文檔
http://technet.microsoft.com/zh-cn/library/ms187074%28SQL.90%29.aspx
他們作用于比較運算符和子查詢之間,作用類似Exists、not exists、in、not in以及其他邏輯意義,這些語法同樣被SQLServer2000支持但是很少看到有人用它們。
復(fù)制代碼 代碼如下:
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 ) --無數(shù)據(jù)
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數(shù)據(jù),那么所有All相關(guān)的比較運算將不會返回任何結(jié)果,原因就不用多解釋了。而因為t1和t2表的null的存在他們和not exists之類的比較符會有一些區(qū)別。
比如下面兩句
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而無法查詢到數(shù)據(jù)。
相關(guān)文章
SQL Server 作業(yè)同步 (結(jié)合備份作業(yè))
昨天發(fā)了篇 SQL Server 作業(yè)備份,今天就加上powershell 把 作業(yè)同步 完善起來,方便需要的朋友2012-06-06SQL Server誤區(qū)30日談 第20天 破壞日志備份鏈之后,需要一個完整備份來重新開始日志鏈
事務(wù)日志備份會備份自上次事務(wù)日志備份以來所有的事務(wù)日志(如果從來沒有過日志備份的話,那就從上一次完整備份開始)。有好幾種類型的操作會中斷事務(wù)日志的連續(xù)性,也就是說除非重新開始新的日志鏈,SQL Server無法再進(jìn)行日志備份2013-01-01自動備份mssql server數(shù)據(jù)庫并壓縮的批處理腳本
windows下,使用mssql命令行工具sqlcmd備份數(shù)據(jù)庫,并調(diào)用rar壓縮;不借助mssql"維護(hù)計劃"功能,拜托權(quán)限問題。2011-07-07數(shù)據(jù)庫初始化及數(shù)據(jù)庫服務(wù)端操作詳解
這篇文章主要為大家介紹了數(shù)據(jù)庫初始化及數(shù)據(jù)庫服務(wù)端操作的過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11SqlServer數(shù)據(jù)庫腳本執(zhí)行命令行指令方式
這篇文章主要介紹了SqlServer數(shù)據(jù)庫腳本執(zhí)行命令行指令方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06