运算符优先级 (Transact-SQL)Operator Precedence (Transact-SQL)

适用于: 是SQL Server是Azure SQL 数据库否Azure SQL 数据仓库否并行数据仓库APPLIES TO: yesSQL Server yesAzure SQL Database noAzure SQL Data Warehouse noParallel Data Warehouse

如果一个复杂表达式有多个运算符,则运算符优先级将确定操作序列。When a complex expression has multiple operators, operator precedence determines the sequence of operations. 执行顺序可能对结果值有明显的影响。The order of execution can significantly affect the resulting value.

运算符的优先级别如下表中所示。Operators have the precedence levels shown in the following table. 在较低级别的运算符之前先对较高级别的运算符进行求值。An operator on higher levels is evaluated before an operator on a lower level. 在下表中,1 代表最高级别,8 代表最低级别。In the following table, 1 is the highest level and 8 is the lowest level.

级别Level 运算符Operators
11 ~(位非)~ (Bitwise NOT)
22 *(乘)、/(除)、%(取模)* (Multiplication), / (Division), % (Modulus)
33 +(正)、-(负)、+(加)、+(串联)、-(减)、&(位与)、^(位异或)、|(位或)+ (Positive), - (Negative), + (Addition), + (Concatenation), - (Subtraction), & (Bitwise AND), ^ (Bitwise Exclusive OR), | (Bitwise OR)
44 =、>、<、>=、<=、<>、!=、!>、!<(比较运算符)=, >, <, >=, <=, <>, !=, !>, !< (Comparison operators)
55 NOTNOT
66 AND
77 ALL、ANY、BETWEEN、IN、LIKE、OR、SOMEALL, ANY, BETWEEN, IN, LIKE, OR, SOME
88 =(赋值)= (Assignment)

如果一个表达式中的两个运算符有相同的优先级别,则按照它们在表达式中的位置对其从左到右进行求值。When two operators in an expression have the same precedence level, they're evaluated left to right based on their position in the expression. 例如,在下面的 SET 语句所使用的表达式中,在加运算符之前先对减运算符进行求值。For example, in the expression that is used in the following SET statement, the subtraction operator is evaluated before the addition operator.

DECLARE @MyNumber int;  
SET @MyNumber = 4 - 2 + 27;  
-- Evaluates to 2 + 27 which yields an expression result of 29.  
SELECT @MyNumber;  

在表达式中使用括号替代所定义的运算符的优先级。Use parentheses to override the defined precedence of the operators in an expression. 对括号内的所有内容进行求值会得到一个单一的值。Everything within parentheses is evaluated to yield a single value. 该值可被括号外的任何运算符使用。That value can be used by any operator outside those parentheses.

例如,在下面的 SET 语句所使用的表达式中,乘运算符具有比加运算符更高的优先级别。For example, in the expression used in the following SET statement, the multiplication operator has a higher precedence than the addition operator. 首先计算乘法运算;表达式结果为 13The multiplication operation is evaluated first; the expression result is 13.

DECLARE @MyNumber int;  
SET @MyNumber = 2 * 4 + 5;  
-- Evaluates to 8 + 5 which yields an expression result of 13.  
SELECT @MyNumber;  

在以下 SET 语句使用的表达式中,括号使加法先进行计算。In the expression used in the following SET statement, the parentheses cause the addition to be evaluated first. 此表达式的结果为 18The expression result is 18.

DECLARE @MyNumber int;  
SET @MyNumber = 2 * (4 + 5);  
-- Evaluates to 2 * 9 which yields an expression result of 18.  
SELECT @MyNumber;  

如果表达式有嵌套的括号,那么首先对嵌套最深的表达式求值。If an expression has nested parentheses, the most deeply nested expression is evaluated first. 以下示例中包含嵌套的括号,其中表达式 5 - 3 在嵌套最深的那对括号中。The following example contains nested parentheses, with the expression 5 - 3 in the most deeply nested set of parentheses. 该表达式产生一个值 2This expression yields a value of 2. 然后,加法运算符 (+) 将此结果与 4 相加,得到值 6Then, the addition operator (+) adds this result to 4 which yields a value of 6. 最后将 62 相乘,生成表达式的结果 12Finally, the 6 is multiplied by 2 to yield an expression result of 12.

DECLARE @MyNumber int;  
SET @MyNumber = 2 * (4 + (5 - 3) );  
-- Evaluates to 2 * (4 + 2) which then evaluates to 2 * 6, and   
-- yields an expression result of 12.  
SELECT @MyNumber;  

另请参阅See Also

逻辑运算符 (Transact-SQL) Logical Operators (Transact-SQL)
运算符 (Transact-SQL) Operators (Transact-SQL)
内置函数 (Transact-SQL)Built-in Functions (Transact-SQL)