Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « 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 »
    comments

    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

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    5161 views
    InstapaperVote on HN

    11 comments

    Comment from: sqlsister [Member] Email
    sqlsister 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: Alex Ullrich [Member] Email
    Alex Ullrich 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: Ted Krueger (onpnt) [Member]
    Ted Krueger (onpnt) 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
    SQLDenis AFAIK VB also has +=
    06/12/08 @ 10:52
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) Really?
    06/12/08 @ 11:30
    Comment from: kaht [Member] Email
    *****
    kaht 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
    superjason 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
    SQLDenis >>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
    ***--
    crzdmn 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
    ****-
    Cyril Gupta 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
    Comment from: Naomi Nosonovsky [Member]
    *****
    Naomi Nosonovsky Nice to know.
    07/13/09 @ 13:37

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

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