SOME | ANY (Transact-SQL)

更新日期: 2006 年 7 月 17 日

比较标量值和单列集中的值。SOME 和 ANY 是等效的。

主题链接图标Transact-SQL 语法约定

语法

scalar_expression { = | < > | ! = | > | > = | ! > | < | < = | ! < } 
     { SOME | ANY } ( subquery ) 

参数

  • scalar_expression
    任何有效的表达式
  • { = | <> | != | > | >= | !> | < | <= | !< }
    任何有效的比较运算符。
  • SOME | ANY
    指定应进行比较。
  • subquery
    包含某列结果集的子查询。所返回列的数据类型必须是与 scalar_expression 相同的数据类型。

结果类型

Boolean

备注

SOME 要求 scalar_expression 与子查询返回的至少一个值比较时满足比较条件。有关要求 scalar_expression 与子查询返回的每个值比较时都符合比较条件的语句,请参阅 ALL (Transact-SQL)。例如,如果子查询返回的值为 2 和 3,则对于值为 2 的 scalar_expressscalar_expression = SOME(子查询)的计算结果为 TRUE。如果子查询返回的值为 2 和 3,则 scalar_expression = ALL(子查询)的计算结果将为 FALSE,因为子查询的某些值(等于 3 的值)不满足表达式的条件。

结果值

对于任何对 (scalar_expression**,**x)(其中 x 是单列集中的值),当指定的比较是 TRUE 时,SOME 或 ANY 返回 TRUE。否则返回 FALSE

示例

以下示例创建一个存储过程,该过程确定是否能够在指定的天数中制造出 AdventureWorks 数据库中具有指定 SalesOrderID 的所有组件。该示例使用子查询为具有特定 SalesOrderID 的所有组件创建 DaysToManufacture 值的列表,然后测试子查询返回的值中是否有大于指定天数的值。如果返回的所有 DaysToManufacture 的值都小于规定的天数,则条件为 TRUE,并输出第一个消息。

USE AdventureWorks ;
GO

CREATE PROCEDURE ManyDaysToComplete @OrderID int, @NumberOfDays int
AS
IF 
@NumberOfDays < SOME
   (
    SELECT DaysToManufacture
    FROM Sales.SalesOrderDetail
    JOIN Production.Product 
    ON Sales.SalesOrderDetail.ProductID = Production.Product.ProductID 
    WHERE SalesOrderID = @OrderID
   )
PRINT 'At least one item for this order cannot be manufactured in specified number of days.'
ELSE 
PRINT 'All items for this order can be manufactured in the specified number of days or less.' ;

若要测试该过程,请使用 SalesOrderID``49080(具有一个需要 2 天的组件和两个需要 0 天的组件)来执行该过程。下面的第一个语句符合条件。第二个查询不符合条件。

EXECUTE DaysToBuild 49080, 2 ;

下面是结果集:

All items for this order can be manufactured in specified number of days or less.

EXECUTE DaysToBuild 49080, 1 ;

下面是结果集:

Some items for this order cannot be manufactured in specified number of days or less.

请参阅

参考

CASE (Transact-SQL)
Functions (Transact-SQL)
运算符 (Transact-SQL)
SELECT (Transact-SQL)
WHERE (Transact-SQL)
IN (Transact-SQL)

其他资源

用 ANY、SOME 或 ALL 修改的比较运算符
子查询基础知识
子查询类型

帮助和信息

获取 SQL Server 2005 帮助

更改历史记录

发布日期 历史记录

2006 年 7 月 17 日

新增内容:
  • 添加了“备注”部分。
  • 添加了示例。