比较运算符 (Transact-SQL)Comparison Operators (Transact-SQL)

适用对象: 是SQL Server 否Azure SQL 数据库 否Azure Synapse Analytics (SQL DW) 否并行数据仓库 APPLIES TO: yesSQL Server noAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

比较运算符测试两个表达式是否相同。Comparison operators test whether two expressions are the same. 除 text、ntext 或 image 数据类型的表达式外,比较运算符可以用于所有其他表达式 。Comparison operators can be used on all expressions except expressions of the text, ntext, or image data types. 下表列出了 Transact-SQLTransact-SQL 比较运算符。The following table lists the Transact-SQLTransact-SQL comparison operators.

运算符Operator 含义Meaning
=(等于)= (Equals) 等于Equal to
>(大于)> (Greater Than) 大于Greater than
<(小于)< (Less Than) 小于Less than
>=(大于或等于)>= (Greater Than or Equal To) 大于或等于Greater than or equal to
<=(小于或等于)<= (Less Than or Equal To) 小于或等于Less than or equal to
<>(不等于)<> (Not Equal To) 不等于Not equal to
!=(不等于)!= (Not Equal To) 不等于(非 ISO 标准)Not equal to (not ISO standard)
!<(不小于)!< (Not Less Than) 不小于(非 ISO 标准)Not less than (not ISO standard)
!>(不大于)!> (Not Greater Than) 不大于(非 ISO 标准)Not greater than (not ISO standard)

Boolean 数据类型Boolean Data Type

具有 Boolean 数据类型的比较运算符的结果 。The result of a comparison operator has the Boolean data type. 这有三个值:TRUE、FALSE 和 UNKNOWN。This has three values: TRUE, FALSE, and UNKNOWN. 返回 Boolean 数据类型的表达式称为布尔表达式 。Expressions that return a Boolean data type are known as Boolean expressions.

与其他 SQL ServerSQL Server 数据类型不同,Boolean 数据类型不能被指定为表列或变量的数据类型,也不能在结果集中返回 。Unlike other SQL ServerSQL Server data types, a Boolean data type cannot be specified as the data type of a table column or variable, and cannot be returned in a result set.

当 SET ANSI_NULLS 为 ON 时,带有一个或两个 NULL 表达式的运算符返回 UNKNOWN。When SET ANSI_NULLS is ON, an operator that has one or two NULL expressions returns UNKNOWN. 当 SET ANSI_NULLS 为 OFF 时,除了等于 (=) 和不等于 (<>) 运算符外,相同的规则适用。When SET ANSI_NULLS is OFF, the same rules apply, except for the equals (=) and not equals (<>) operators. 当 SET ANSI_NULLS 为 OFF 时,这些运算符将 NULL 视为已知值,等效于任何其他 NULL,且只返回 TRUE 或 FALSE(绝不会返回 UNKNOWN)。When SET ANSI_NULLS is OFF, these operators treat NULL as a known value, equivalent to any other NULL, and only return TRUE or FALSE (never UNKNOWN).

在 WHERE 子句中使用数据类型为 Boolean 的表达式,可以筛选出符合搜索条件的行,也可以在控制流语言语句(例如 IF 和 WHILE)中使用这种表达式。例如: Expressions with Boolean data types are used in the WHERE clause to filter the rows that qualify for the search conditions and in control-of-flow language statements such as IF and WHILE, for example:

-- Uses AdventureWorks  
  
DECLARE @MyProduct int;  
SET @MyProduct = 750;  
IF (@MyProduct <> 0)  
   SELECT ProductID, Name, ProductNumber  
   FROM Production.Product  
   WHERE ProductID = @MyProduct;  

另请参阅See Also

表达式 (Transact-SQL)Expressions (Transact-SQL)
运算符 (Transact-SQL)Operators (Transact-SQL)