A few days ago, The Impatient DBA wrote that in Microsoft SQL Server 2008, MS added an old C operator: +=. Simply put, add a value to a variable. You can run the following code that he provided:
@shamwow += 1;
@shamwow += 1;
And @shamwow is now 2. Without using the += operator, we’d be doing @shamwow = @shamwow +1. Is += better? It is certainly shorter, but I think that’s mainly a matter of personal choice. If you code in other languages that support it, I think it would probably be a good thing.
But there’s more than just adding: there’s also operators for the other basic math functions: -=, *=, /=. Let’s continue playing with @shamwow with the following code:
DECLARE @shamwow FLOAT = 3; SET @shamwow *= 3; SELECT @shamwow; SET @shamwow /= 2; SELECT @shamwow; SET @shamwow -= 3; SELECT @shamwow;
SSMS will return 9, 4.5, and 1.5.
Pretty cool, eh?
But there’s two problems. First, this is supported in T-SQL, but not in Access 2013 VBA. The other is that there’s another pair of C operators, ++ and — (that’s minus minus, not an em dash). Add or subtract one from the preceding variable, so you’d do set @shamwow++ if it were supported. And these latter two are not supported in either T-SQL or VBA. I’m sure they’re alive and well in C# and probably several other .Net languages.
I’m frankly amazed that this happened EIGHT YEARS AGO and I haven’t seen it in common usage. It’s been in C for probably as long as there has been C, and now we have it in T-SQL. Something about new tricks and old dogs comes to mind.