August 11, 2011

Compound Assignment Operators in SQL Server 2008

Compound assignment operators are newly introduced in SQL Server 2008 and the code certainly looks cleaner and easier to type while using them. I am surprised why weren’t they introduced in earlier SQL Server versions at the first place, however now that they are here, let’s quickly learn how to use them:

Observe the following piece of code

SQL Server Compound Assignment Operator

As you can see, we have used a compound assignment operator to simplify the syntax of adding 1 to the existing value of @num and assigning the result to @num. Although this may look as it is not needed, it is extremely useful where you have T-SQL code which does many arithmetic operations on variables.

Here are a couple of other Compound Assignment Operators for your reference:

+=    Add
-=    Subtract
*=    Multiply
/=    Divide
^=    Bitwise XOR
|=    Bitwise OR
%=   Modulo
&=   Bitwise AND

About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

No comments: