Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted Jun 4, 2008

Compound Assignment Operators in SQL Server 2008

By Muthusamy Anantha Kumar aka The MAK

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



MS SQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date