OR (Transact-SQL)OR (Transact-SQL)

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

将两个条件组合起来。Combines two conditions. 在一个语句中使用多个逻辑运算符时,在 AND 运算符之后对 OR 运算符求值。When more than one logical operator is used in a statement, OR operators are evaluated after AND operators. 不过,使用括号可以更改求值的顺序。However, you can change the order of evaluation by using parentheses.

主题链接图标 TRANSACT-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

boolean_expression OR boolean_expression  

参数Arguments

boolean_expressionboolean_expression
返回 TRUE、FALSE 或 UNKNOWN 的任何有效表达式Is any valid expression that returns TRUE, FALSE, or UNKNOWN.

结果类型Result Types

BooleanBoolean

结果值Result Value

当两个条件中的任何一个为 TRUE 时,OR 返回 TRUE。OR returns TRUE when either of the conditions is TRUE.

RemarksRemarks

下表显示 OR 运算符的结果。The following table shows the result of the OR operator.

TRUETRUE FALSEFALSE UNKNOWNUNKNOWN
TRUETRUE TRUETRUE TRUETRUE TRUETRUE
FALSEFALSE TRUETRUE FALSEFALSE UNKNOWNUNKNOWN
UNKNOWN UNKNOWN TRUETRUE UNKNOWNUNKNOWN UNKNOWNUNKNOWN

示例Examples

以下示例使用 vEmployeeDepartmentHistory 视图检索加晚班或夜班的 Quality Assurance 人员的姓名。The following example uses the vEmployeeDepartmentHistory view to retrieve the names of Quality Assurance personnel who work either the evening shift or the night shift. 如果省略括号,查询将返回加晚班的 Quality Assurance 雇员和所有加夜班的雇员。If the parentheses are omitted, the query returns Quality Assurance employees who work the evening shift and all employees who work the night shift.

-- Uses AdventureWorks  
  
SELECT FirstName, LastName, Shift   
FROM HumanResources.vEmployeeDepartmentHistory  
WHERE Department = 'Quality Assurance'  
   AND (Shift = 'Evening' OR Shift = 'Night');  

下面是结果集:Here is the result set.

FirstName    LastName         Shift 
------------ ---------------- ------- 
Andreas      Berglund         Evening 
Sootha       Charncherngkha   Night

示例:Azure Synapse Analytics (SQL DW)Azure Synapse Analytics (SQL DW)并行数据仓库Parallel Data WarehouseExamples: Azure Synapse Analytics (SQL DW)Azure Synapse Analytics (SQL DW) and 并行数据仓库Parallel Data Warehouse

以下示例检索所得的 BaseRate 少于 20 或 HireDate 在 2001 年 1 月 1 日或在此之后的员工的姓名。The following example retrieves the names of employees who either earn a BaseRate less than 20 or have a HireDate January 1, 2001 or later.

-- Uses AdventureWorks  
  
SELECT FirstName, LastName, BaseRate, HireDate   
FROM DimEmployee  
WHERE BaseRate < 10 OR HireDate >= '2001-01-01';  

另请参阅See Also

表达式 (Transact-SQL) Expressions (Transact-SQL)
内置函数 (Transact-SQL) Built-in Functions (Transact-SQL)
运算符 (Transact-SQL) Operators (Transact-SQL)
SELECT (Transact-SQL) SELECT (Transact-SQL)
WHERE (Transact-SQL)WHERE (Transact-SQL)