複合運算子 (Transact-SQL)Compound Operators (Transact-SQL)

適用範圍:Applies to: 是SQL ServerSQL Server (所有支援的版本) yesSQL ServerSQL Server (all supported versions) 是Azure SQL DatabaseAzure SQL DatabaseYesAzure SQL DatabaseAzure SQL Database適用範圍:Applies to: 是SQL ServerSQL Server (所有支援的版本) yesSQL ServerSQL Server (all supported versions) 是Azure SQL DatabaseAzure SQL DatabaseYesAzure SQL DatabaseAzure SQL Database

複合運算子會執行某項作業,然後將原始值設定為該作業的結果。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.
&= &= (位元 AND 指派) (Transact-SQL)&= (Bitwise AND Assignment) (Transact-SQL) 執行位元運算 AND,然後將原始值設為該結果。Performs a bitwise AND and sets the original value to the result.
^= ^= (位元排除 OR 指派) (Transact-SQL)^= (Bitwise Exclusive OR Assignment) (Transact-SQL) 執行位元排除 OR,然後將原始值設為該結果。Performs a bitwise exclusive OR and sets the original value to the result.
|=|= |= (位元 OR 指派) (Transact-SQL)|= (Bitwise OR Assignment) (Transact-SQL) 執行位元運算 OR,然後將原始值設為該結果。Performs a bitwise OR and sets the original value to the result.

語法Syntax

  
expression operator expression  

注意

若要檢視 SQL Server 2014 與更早版本的 Transact-SQL 語法,請參閱舊版文件To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.

引數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)