Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

XML Feeds

« How to rename a column in a SQL Server table without using the designerSQL Server 2008 Adds ISO Week,TZoffset, Nanosecond And Microsecond To Datepart »
The Data Management Journal

Compound Operators Or How T-SQL Is Morphing Into VB Or C#

by SQLDenis


Permalink 12 Jun 2008 09:19 , Categories: Data Modelling & Design

C# is becoming more dynamic (like Python and Ruby) but now SQL is becoming more like C# (but with LINQ C# is becoming more like SQL)
Confused? Good!

SQL Server has added compound operators. Instead of writing

  1. SET @value = @value + 100

you can just do

  1. SET @value += 100

See that? I can see a lot of people writing Dynamic SQL already smiling. Really who wants to write code like this whole day long?

  1. SET @MyBigDynamicSQLString = MyBigDynamicSQLString + ‘ From ‘ + @Table

This is much shorter (but not better IMNSHO)

  1. SET @MyBigDynamicSQLString +=  ’ From ‘ + @Table

Anyway here are all the compound operators that you can use:

+= (Add EQUALS)
Adds some amount to the original value and sets the original value to the result.

-= (Subtract EQUALS)
Subtracts some amount from the original value and sets the original value to the result.

*= (Multiply EQUALS)
Multiplies by an amount and sets the original value to the result.

/= (Divide EQUALS)
Divides by an amount and sets the original value to the result.

%= (Modulo EQUALS)
Divides by an amount and sets the original value to the modulo.

&= (Bitwise AND EQUALS)
Performs a bitwise AND and sets the original value to the result.

^= (Bitwise Exclusive OR EQUALS)
Performs a bitwise exclusive OR and sets the original value to the result.

|= (Bitwise OR EQUALS)
Performs a bitwise OR and sets the original value to the result.

Have fun reconstructing your strings :-)

Here is another example where SQL is morphing

  1. DECLARE @find varchar(30);
  2. SET @find = ‘Man%’;

In SQL Server 2008 you can do

  1. DECLARE @find varchar(30) = ‘Man%’;


Much nicer

10 comments »1 trackback » 1813 views

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

10 comments, 1 trackback

Comment from: sqlsister [Member] Email
I guess I'm getting old. I see no particular reason why this change is an
improvement or even needed. Change for the sake of change is silly. But then I
never found the old way to be difficult to learn or use. (And I would feel the person who did find the concept difficult to be someone who doesn't
belong in the IT world.)
06/12/08 @ 10:00
Comment from: AlexCuse [Member] Email
I don't care much for the +=, -=, etc... I'm a huge fan of the better variable declaration though.
06/12/08 @ 10:17
Comment from: onpnt [Member] Email
I was sitting here a long time ago reading the new pocket guide on 2008 roughly going over several changes like forceseek, the extending of SQLCLR, backup compression and on until I got to page 28..started reading on row constructors and then flipped the page and saw a table with the first column "Operator" and when I saw += *sigh*, this big smile came over my face.

You know it may not be needed but if anything it saves time in writing non-vb looking ugly assignments ;)

I'm for it and happy to see it finally changed.
06/12/08 @ 10:48
Comment from: SQLDenis [Member] Email
AFAIK VB also has +=
06/12/08 @ 10:52
Comment from: chrissie1 [Member] Email
Really?
06/12/08 @ 11:30
Comment from: kaht [Member] Email
*****
Sweet!

The lack of a += operator has been one of the few things that has bugged me when I write SQL.
06/12/08 @ 12:39
Comment from: superjason [Visitor] · http://www.ytechie.com
What version is this coming to?

I LOVE the operators. Why do you want to double type your variable names? That just seems like a waste of time.

As far as VB having +=, I believe VB.NET had it, but not VB.
06/13/08 @ 11:12
Comment from: SQLDenis [Member] Email
>>I believe VB.NET had it, but not VB.

Hahah but VB.NET got renamed to VB a couple of years ago

So when I said VB I meant the VB that runs on top of .NET (CLR) not VB6 or earlier
06/13/08 @ 11:16
Comment from: crzdmn [Visitor] Email
***--
People write new applications in VB? Have no business learned? Or is it the price tag that comes with a VB programmer vs a REAL programmer.
06/13/08 @ 22:37
Comment from: Cyril Gupta [Visitor] · http://www.cyrilgupta.com
****-
Okay, now how many things do you expect me to learn in a lifetime?

I know just enough SQL to survive.

Banzai!
06/14/08 @ 08:08
Trackback from: Pythian Group Blog [Visitor]
Log Buffer #102: a Carnival of the Vanities for DBAs
Welcome to the 102nd edition of Log Buffer, the weekly review of database blogs.
Since it was DB2’s 25th birthday this week, as Anant Jhingran reports, let’s start with it.
From ZDNet this week came a story that IBM was considering the open...
06/20/08 @ 13:34

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)