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
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4157 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 Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 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
SSChasing Mays
SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)

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

Group: General Forum Members
Points: 46 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
SSC Rookie
SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)SSC Rookie (40 reputation)

Group: General Forum Members
Points: 40 Visits: 51
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 (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)SSC Guru (64K reputation)

Group: General Forum Members
Points: 64223 Visits: 18570
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

Ed Wagner
Ed Wagner
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45403 Visits: 10835
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
SSChasing Mays
SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)

Group: General Forum Members
Points: 645 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.B
Alan.B
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12869 Visits: 7996
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



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't 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. " -- Itzek 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