Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
SQL database in Microsoft Fabric
Compound operators execute some operation and set an original value to the result of the operation. 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-SQL provides the following compound operators:
Operator | Link to more information | Action |
---|---|---|
+= | += (Add Assignment) (Transact-SQL) | Adds some amount to the original value and sets the original value to the result. |
-= | -= (Subtract Assignment) (Transact-SQL) | Subtracts some amount from the original value and sets the original value to the result. |
*= | *= (Multiply Assignment) (Transact-SQL) | Multiplies by an amount and sets the original value to the result. |
/= | (Divide Assignment) (Transact-SQL) | Divides by an amount and sets the original value to the result. |
%= | Modulus Assignment (Transact-SQL) | Divides by an amount and sets the original value to the modulo. |
&= | &= (Bitwise AND Assignment) (Transact-SQL) | Performs a bitwise AND and sets the original value to the result. |
^= | ^= (Bitwise Exclusive OR Assignment) (Transact-SQL) | Performs a bitwise exclusive OR and sets the original value to the result. |
|= | |= (Bitwise OR Assignment) (Transact-SQL) | Performs a bitwise OR and sets the original value to the result. |
expression operator expression
expression
Is any valid expression of any one of the data types in the numeric category.
Returns the data type of the argument with the higher precedence. For more information, see Data Type Precedence (Transact-SQL).
For more information, see the topics related to each operator.
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;
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register today