Friday 19 October 2012

COMPOUND OPERATORS



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).

  • Add Equals “+=”
  • Subtract Equals “-=”
  • Multiply Equals “*=”
  • Divide Equals “/=”
  • Modulo Equals “%=”
  • Bitwise AND Equals “&=”
  • Bitwise OR Equals “|=”
  • Bitwise Exclusive OR Equals “^=”

Add Equals (+=): Adds some amount to the original value and sets the original value to the result.

Example: DECLARE @v1 INT;
SET @v1 = 10;
SET @v1 += 2;
select @v1;


Subtract Equals (-=): Subtracts some amount from the original value and sets the original value to the result.
Example: DECLARE @v1 INT;
SET @v1 = 10;
SET @v1 -= 2;
select @v1;


Multiply Equals (*=): Multiplies by an amount and sets the original value to the result.

Example: DECLARE @v1 INT;
SET @v1 = 10;
SET @v1 *= 2;
select @v1;


Divide Equals (/=): Divides by an amount and sets the original value to the result.
Example: DECLARE @v1 INT;
SET @v1 = 10;
SET @v1 /= 2;
select @v1;


Modulo Equals (%=): Divides by an amount and sets the original value to the modulo.

Example: DECLARE @v1 INT;
SET @v1 = 15;
SET @v1 %= 2;
select @v1;


Bitwise AND Equals (&=): Performs a bitwise AND and sets the original value to the result.

Example: DECLARE @v1 INT;
SET @v1 = 10;
SET @v1 &= 2;
select @v1;


Bitwise OR Equals (|=): Performs a bitwise OR and sets the original value to the result.

Example: DECLARE @v1 INT;
SET @v1 = 10;
SET @v1 |= 2;
select @v1;
 
Bitwise Exclusive OR Equals (^=): Performs a bitwise exclusive OR and sets the original value to the result.

Example: DECLARE @v1 INT;
SET @v1 = 10;
SET @v1 ^= 2;
select @v1;


No comments:

Post a Comment