ALL (Transact-SQL)

更新日期: 2006 年 7 月 17 日

比较标量值和单列集中的值。

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

语法

scalar_expression { = | <> | != | > | >= | !> | < | <= | !< } ALL ( subquery )

参数

  • scalar_expression
    任何有效的表达式
  • { = | <> | != | > | >= | !> | < | <= | !< }
    比较运算符。
  • subquery
    返回单列结果集的子查询。返回列的数据类型必须与 scalar_expression 的数据类型相同。

    受限的 SELECT 语句,其中不允许使用 ORDER BY 子句、COMPUTE 子句和 INTO 关键字。

结果类型

Boolean

备注

ALL 要求 scalar_expression 与子查询返回的每个值进行比较时都应满足比较条件。例如,如果子查询返回的值为 2 和 3,则对于值为 2 的 scalar_expressionscalar_expression <= ALL(子查询)的计算结果为 TRUE。如果子查询返回的值为 2 和 3,则 scalar_expression = ALL(子查询)的计算结果为 FALSE,因为子查询的某些值(等于 3 的值)不满足表达式的条件。

有关要求 scalar_expression 只与子查询返回的某一个值进行比较时满足比较条件的语句,请参阅 SOME | ANY (Transact-SQL)

本主题讨论了 ALL 用于子查询的情况。ALL 也可以与 UNIONSELECT 一起使用。

结果值

如果对于所有比较对 (scalar_expression**,**x) 指定的比较均为 TRUE(其中 x 是单列集中的值),则返回 TRUE;否则返回 FALSE。

示例

以下示例创建一个存储过程,该过程确定是否能够在指定的天数中制造出 AdventureWorks 数据库中具有指定 SalesOrderID 的所有组件。该示例使用子查询为具有特定 SalesOrderID 的所有组件创建 DaysToManufacture 值的列表,然后确认所有 DaysToManufacture 都在指定的天数内。

USE AdventureWorks ;
GO

CREATE PROCEDURE DaysToBuild @OrderID int, @NumberOfDays int
AS
IF 
@NumberOfDays >= ALL
   (
    SELECT DaysToManufacture
    FROM Sales.SalesOrderDetail
    JOIN Production.Product 
    ON Sales.SalesOrderDetail.ProductID = Production.Product.ProductID 
    WHERE SalesOrderID = @OrderID
   )
PRINT 'All items for this order can be manufactured in specified number of days or less.'
ELSE 
PRINT 'Some items for this order cannot be manufactured in 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)
表达式(Transact-SQL)
Functions (Transact-SQL)
LIKE (Transact-SQL)
运算符 (Transact-SQL)
SELECT (Transact-SQL)
WHERE (Transact-SQL)
IN (Transact-SQL)

其他资源

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

帮助和信息

获取 SQL Server 2005 帮助

更改历史记录

发布日期 历史记录

2006 年 7 月 17 日

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