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 SQL Server Compound Assignment Operator](http://lh5.ggpht.com/-DyuNy77zKLg/TkS4ZMbYpYI/AAAAAAAACJA/aPk873rRZyk/image%25255B2%25255D.png?imgmax=800)
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
-= Subtract
*= Multiply
/= Divide
^= Bitwise XOR
|= Bitwise OR
%= Modulo
&= Bitwise AND
No comments:
Post a Comment