SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stairway to T-SQL: Beyond The Basics Level 8: Coding Shortcuts using += and -= Operators


Stairway to T-SQL: Beyond The Basics Level 8: Coding Shortcuts using += and -= Operators

Author
Message
Greg Larsen
Greg Larsen
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10067 Visits: 290
Comments posted to this topic are about the item Stairway to T-SQL: Beyond The Basics Level 8: Coding Shortcuts using += and -= Operators

Gregory A. Larsen, MVP
klaus.kuehne
klaus.kuehne
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 17
Hello,

it's worth to be mentioned that ++ and -- operators don't exist in TSQL. The following example code:

declare @ int = 1, @j int, @k int
Set @j = ++@i
Set @k = @i++
Print @i, @j, @k

will be executed without an error, but @j and @k get content "1".

It seems that "++@i" evaluates to "+(+(@i))" ==> "0+0+@i" ==> 1 and "@i++" to "@i+0+0" ==> 1.

Regards,

Klaus Kuehne
Robert.Sterbal
Robert.Sterbal
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1177 Visits: 2000
The -- operator would cause quite an issue, since that is the comment mark
klaus.kuehne
klaus.kuehne
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 17
Indeed. "set @j = @i--" is also a nice joke. Good idea for placing some T-SQL bombs in our companies Smile
m2c2
m2c2
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 65
I've been at this for a bit now, and I don't see the need for the "=" part of these operations. It would seem that "+" and "-" perform the same functions by themselves.

Maybe I'm overlooking something basic? My wife always asks why I can't see things in our fridge that turn up in clear view...after she points them out. Blink
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)

Group: General Forum Members
Points: 144293 Visits: 18651
CELKO (7/9/2014)
These shorthands are a bad idea.


I'm gonna agree with that. I don't like seeing shorthand in the code.

Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw
Learn Extended Events

Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)SSC Guru (156K reputation)

Group: General Forum Members
Points: 156925 Visits: 11656
SQLRNNR (7/9/2014)
CELKO (7/9/2014)
These shorthands are a bad idea.


I'm gonna agree with that. I don't like seeing shorthand in the code.

I'm with you - I don't like it. I have to admit that until I read that article, I've never even seen them before. I agree with the premise that nobody wants to type more than they have to, but I believe whole-heartedly that readability and maintainability are vastly more important than typing a few extra characters.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Robert.Sterbal
Robert.Sterbal
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1177 Visits: 2000
we could use a like button on this forum... on all the posts that are saying this is an unnecessary extension to t-sql
Alan Burstein
Alan Burstein
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32430 Visits: 8576
It's worth noting that these are referred to as Compound Assignment Operators and you can use them for multiplication (*=), division ( /=), modulo (%=), and a few bitwise operations (note the link above).

Its interesting to see the comments - not a a lot of love here apparently for them but I do like them personally ( style choice perhaps).

-- Alan Burstein


Helpful links:

Best practices for getting help on SQLServerCentral -- Jeff Moden
How to Post Performance Problems -- Gail Shaw

Nasty fast set-based string manipulation functions:
For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)
To split strings based on patterns try PatternSplitCM
Need to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8K

I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search