Pages

Thursday, November 10, 2011

Compound Assignment Operators in SQL Server 2008

SQL SERVER 2008 has introduced new concept of Compound Assignment Operators. Compound Assignment Operators are available in many other programming languages for quite some time. Compound Assignment Operators is operator where variables are operated upon and assigned on the same line.



The new operators are:

  •  += (plus equals)
  •  -=  (minus equals)
  • *=  (multiplication equals)
  •  /=  (division equals)
  • %=  (modulo equals)

You can use these operators wherever assignment is normally allowed—for example, in the SET clause of an UPDATE statement or in a SET statement that assigns values to variables. The following code example demonstrates the use of the += operator:


DECLARE @price AS MONEY = 10.00;
SET @price += 2.00;
SELECT @price;


This code sets the variable @price to its current value, 10.00, plus 2.00, resulting in 12.00.

No comments: