复合运算符 (Transact-SQL)Compound Operators (Transact-SQL)

适用于: 是SQL Server 是Azure SQL 数据库 否Azure Synapse Analytics (SQL DW) 否并行数据仓库 APPLIES TO: YesSQL Server YesAzure SQL Database NoAzure Synapse Analytics (SQL DW) NoParallel Data Warehouse

复合运算符执行一些运算并将原始值设置为运算的结果。Compound operators execute some operation and set an original value to the result of the operation. 例如,如果变量 @x 等于 35,则 @x += 2 会将 @x 的原始值加上 2 并将 @x 设置为该新值 (37)。For example, if a variable @x equals 35, then @x += 2 takes the original value of @x, add 2 and sets @x to that new value (37).

Transact-SQLTransact-SQL 提供了以下复合运算符:provides the following compound operators:

操作员Operator 详细信息链接Link to more information 操作Action
+= +=(加法赋值)(Transact-SQL)+= (Add Assignment) (Transact-SQL) 将原始值加上一定的量,并将原始值设置为结果。Adds some amount to the original value and sets the original value to the result.
-= -=(减法赋值)(Transact-SQL)-= (Subtract Assignment) (Transact-SQL) 将原始值减去一定的量,并将原始值设置为结果。Subtracts some amount from the original value and sets the original value to the result.
*= *=(乘法赋值)(Transact-SQL)*= (Multiply Assignment) (Transact-SQL) 将原始值乘上一定的量,并将原始值设置为结果。Multiplies by an amount and sets the original value to the result.
/= (除法赋值)(Transact-SQL)(Divide Assignment) (Transact-SQL) 将原始值除以一定的量,并将原始值设置为结果。Divides by an amount and sets the original value to the result.
%= 取模赋值 (Transact-SQL)Modulus Assignment (Transact-SQL) 将原始值除以一定的量,并将原始值设置为余数。Divides by an amount and sets the original value to the modulo.
&= &=(位与赋值)(Transact-SQL)&= (Bitwise AND Assignment) (Transact-SQL) 对原始值执行位与运算,并将原始值设置为结果。Performs a bitwise AND and sets the original value to the result.
^= ^=(位异或赋值)(Transact-SQL)^= (Bitwise Exclusive OR Assignment) (Transact-SQL) 对原始值执行位异或运算,并将原始值设置为结果。Performs a bitwise exclusive OR and sets the original value to the result.
|=|= |=(位或赋值)(Transact-SQL)|= (Bitwise OR Assignment) (Transact-SQL) 对原始值执行位或运算,并将原始值设置为结果。Performs a bitwise OR and sets the original value to the result.

语法Syntax

  
expression operator expression  

参数Arguments

expressionexpression
数值类别中任意一种数据类型的任意有效表达式Is any valid expression of any one of the data types in the numeric category.

结果类型Result Types

返回优先级较高的参数的数据类型。Returns the data type of the argument with the higher precedence. 有关详细信息,请参阅数据类型优先级 (Transact-SQL)For more information, see Data Type Precedence (Transact-SQL).

备注Remarks

有关详细信息,请参阅与每个运算符相关的主题。For more information, see the topics related to each operator.

示例Examples

下面的示例演示复合运算。The following examples demonstrate compound operations.

DECLARE @x1 int = 27;  
SET @x1 += 2 ;  
SELECT @x1 AS Added_2;  
  
DECLARE @x2 int = 27;  
SET @x2 -= 2 ;  
SELECT @x2 AS Subtracted_2;  
  
DECLARE @x3 int = 27;  
SET @x3 *= 2 ;  
SELECT @x3 AS Multiplied_by_2;  
  
DECLARE @x4 int = 27;  
SET @x4 /= 2 ;  
SELECT @x4 AS Divided_by_2;  
  
DECLARE @x5 int = 27;  
SET @x5 %= 2 ;  
SELECT @x5 AS Modulo_of_27_divided_by_2;  
  
DECLARE @x6 int = 9;  
SET @x6 &= 13 ;  
SELECT @x6 AS Bitwise_AND;  
  
DECLARE @x7 int = 27;  
SET @x7 ^= 2 ;  
SELECT @x7 AS Bitwise_Exclusive_OR;  
  
DECLARE @x8 int = 27;  
SET @x8 |= 2 ;  
SELECT @x8 AS Bitwise_OR;  
  

另请参阅See Also

运算符 (Transact-SQL) Operators (Transact-SQL)
位运算符 (Transact-SQL)Bitwise Operators (Transact-SQL)