Archive for February 21st, 2011

T-SQL Compound Assignment operators February 21st, 2011

Vinod Kumar

The SQL Server 2008 T-SQL language has been extended to allow compound assignment operators (e.g., addition assignment, subtraction assignment, etc). This can greatly reduce the amount of code and may boost productivity and code readability. Compound assignments may be used with SET, SELECT, and any other place where an assignment is used such as an UPDATE statement.

I personally love to see these getting used, but on a contrary also feel this might make our good old T-SQL champs into a C# programmer for SQL Server. But there is so much of elegance which can be used in this new era of programming.


Add the two values and assign


Subtract the value and assign


Multiply the values and assign


Divide by the value and assign


Modulo and assign


Bitwise AND and assign


Bitwise XOR and assign


Bitwise OR and assign

Some of the typical examples of usage are as below:

DECLARE @var1 VARCHAR(30) = ‘Hello’
SELECT @var1 += ‘ World’
SELECT @var1

SET    c1 += 10 

So how many of you use these syntax in your production code? Would love to hear from you.

Continue reading...