Compound Assignment Operators in SQL Server 2008

We have seen and used compound assignment
operators in procedural languages like C++ and C# etc. Now this Compound Assignment
Operator feature and enhancement is introduced in SQL Server 2008.

What is compound assignment operator?

Compound assignment operator means an
operator combined with another operator.

The compound assignment operators that
are supported in SQL Server 2008 are:

Compound Assignment Operator

Description

+=

Add and assign

-=

Subtract and assign

*=

Multiply and assign

/=

Divide and assign

%=

Modulus and assign

&=

Bitwise AND and assign

|=

Bitwise OR and assign

^=

Bitwise XOR and assign

Compound Assignment Operator – Add and Assign
Example

Let’s use the Add and assign compound assignment operator as shown
below.


Declare @myvariable int
Set @myvariable = 10
–using Compound assignment operator
Set @myvariable+=100
Select @myvariable as MyResult
Go

Result


MyResult
110
(1 row(s) affected)

In the above example, the compound assignment operator added the value
100 to the existing value of the variable @myvariable and assigned the
resulting value to @myvariable.

This could also be re-written as shown below.


Declare @myvariable int
Set @myvariable = 10
–Not using Compound assignment operator
Set @myvariable=@myvariable+100
Select @myvariable as MyResult
Go

Result


MyResult
110
(1 row(s) affected)

Compound Assignment Operator – Subtract and Assign
Example

Let’s use the Subtract and assign compound assignment operator as
shown below.


Declare @myvariable int
Set @myvariable = 10
–using Compound assignment operator
Set @myvariable-=3
Select @myvariable as MyResult
Go

Result


MyResult
7
(1 row(s) affected)

In the above example, the compound assignment operator subtracted the
value 3 from the existing value of the variable @myvariable and assigned the
resulting value to @myvariable.

This could also be re-written as shown below.


Declare @myvariable int
Set @myvariable = 10
— Not using Compound assignment operator
Set @myvariable=@myvariable-3
Select @myvariable as MyResult
Go

Result


MyResult
7
(1 row(s) affected)

Compound Assignment Operator – Multiply and
assign Example

Let’s use the Multiply and assign compound assignment operator as
shown below.


Declare @myvariable int
Set @myvariable = 10
–using Compound assignment operator
Set @myvariable*=5
Select @myvariable as MyResult
Go

Result


MyResult
50
(1 row(s) affected)

In the above example, the compound assignment operator multiplied the
value 5 with the existing value of the variable @myvariable and assigned the
resulting value to @myvariable.

This could also be re-written as shown below


Declare @myvariable int
Set @myvariable = 10
— Not using Compound assignment operator
Set @myvariable=@myvariable*5
Select @myvariable as MyResult
Go

Result


MyResult
50
(1 row(s) affected)

Compound Assignment Operator – Divide and Assign
Example

Let’s use the Divide and assign compound assignment operator as shown
below.


Declare @myvariable decimal(5,2)
Set @myvariable = 5
–using Compound assignment operator
Set @myvariable/=2
Select @myvariable as MyResult
Go

Result


MyResult
2.50
(1 row(s) affected)

In the above example, the compound assignment operator
divided the existing value of the variable @myvariable byt the value of 2 and
assigned the resulting value to @myvariable.

This could also be re-written as shown below.


Declare @myvariable decimal(5,2)
Set @myvariable = 5
— Not using Compound assignment operator
Set @myvariable=@myvariable/2
Select @myvariable as MyResult
Go

Result


MyResult
2.50
(1 row(s) affected)

Compound Assignment Operator – Divide and Assign
Example

Let’s use the Modulus and assign compound assignment operator as shown
below.


Declare @myvariable int
Set @myvariable = 5
–using Compound assignment operator
Set @myvariable %=2
Select @myvariable as MyResult
Go

Result


MyResult
1
(1 row(s) affected)

In the above example, the compound assignment operator
divided the existing value of the variable by the value of 2 and the reminded
value is assigned as the value to @myvariable.

This could also be re-written as shown below.


Declare @myvariable int
Set @myvariable = 5
— Not using Compound assignment operator
Set @myvariable=@myvariable%2
Select @myvariable as MyResult
Go

Result


MyResult
1
(1 row(s) affected)

Compound Assignment Operator – Bit Wise Operators

Let’s use the compound bit wise AND assignment operator as shown
below.


Declare @myvariable int
Set @myvariable = 100
–using Compound assignment operator
Set @myvariable &=1000
Select @myvariable as MyResult
Go

Result


MyResult
96
(1 row(s) affected)

This could also be re-written as shown below.


Declare @myvariable int
Set @myvariable = 100
–Not using Compound assignment operator
Set @myvariable=@myvariable&1000
Select @myvariable as MyResult
Go

Result


MyResult
96
(1 row(s) affected)

Let’s use the compound bit wise OR assignment operator as shown below.


Declare @myvariable int
Set @myvariable = 100
–using Compound assignment operator
Set @myvariable |=1000
Select @myvariable as MyResult
Go

Result


MyResult
1004
(1 row(s) affected)

This could also be re-written as shown below.


Declare @myvariable int
Set @myvariable = 100
–Not using Compound assignment operator
Set @myvariable=@myvariable|1000
Select @myvariable as MyResult
Go

Result


MyResult
1004
(1 row(s) affected)

Let us use the compound bit wise XOR assignment operator as shown
below.


Declare @myvariable int
Set @myvariable = 100
–using Compound assignment operator
Set @myvariable ^=1000
Select @myvariable as MyResult
Go

Result


MyResult
908
(1 row(s) affected)

This could also be re-written as shown below.


Declare @myvariable int
Set @myvariable = 100
–Not using Compound assignment operator
Set @myvariable=@myvariable^1000
Select @myvariable as MyResult
Go

Result


MyResult
908
(1 row(s) affected)

Conclusion

This article has illustrated the use of Compound Assignment Operator that
is going to be supported in SQL Server 2008.

Note: Examples in this article used SQL Server 2008 CTP6

»


See All Articles by Columnist
MAK

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles