Forgotten SQL Server comparison operator modifiers

Source: Internet
Author: User
Tags comparison modifiers

There are three keywords in SQL Server that can modify comparison operators: All, any, and some, where some and any are equivalent.
The official reference document
http://technet.microsoft.com/zh-cn/library/ms187074%28sql.90%29.aspx
They work between comparison operators and subqueries, The function is similar to exists, not exists, in and not, and other logical meanings, which are also supported by SQLServer2000 but are rarely seen to be used by anyone.

Copy code The code is as follows:
SET NOCOUNT on
use tempdb
Go
if (object_id (' T1 ') are NOT null) drop table T1
Crea Te table t1 (n int)
INSERT INTO T1 select 2 Union Select 3
if (object_id (' T2 ') are 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-T1)--4
Select * from T2 where n> No (select n from t1)--3,4
--select * fr Om T2 where N>some (select n from t1)--3,4
Select * to T2 where n= all (select n from T1)--No data
SELECT * F Rom T2 where n= any (select N-T1)--2,3
--select * from T2 where n=some (select n from t1)--2,3
Select * F Rom T2 where n< all (select N-T1)--1
Select * from T2 where n< any (select n from t1)--1,2
--sele CT * from T2 where N<some (SeleCT n-T1)--1,2
Select * from T2 where n<> all (select N-T1)--1,4
Select * from T2 where n<& Gt Any (select N-T1)--1,2,3,4
--select * from T2 where n<>some (select N-T1)--1,2,3,4
SET NOCOUNT Off


Note that if the T1 contains null data, then all the all-related comparison operations will not return any results, so there is no reason to explain. And because of the existence of NULL in T1 and T2 tables, there are differences between them and the comparison characters such as not exists.

such as the following two sentences
select * from T2 a Where does exists (select 1 from t1 where N>=A.N)
Sele CT * from T2 where n >all (select n from t1)
They are logically similar but the processing of NULL is quite the opposite, and the first sentence ignores the null of the subquery and detects the null of the T2. The second sentence ignores T2 null and cannot query data because of NULL in T1.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.