Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 1234»»»

Stairway to T-SQL: Beyond The Basics Level 8: Coding Shortcuts using += and -= Operators Expand / Collapse
Author
Message
Posted Thursday, May 29, 2014 6:36 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, August 17, 2016 10:41 PM
Points: 1,043, 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
Post #1575920
Posted Wednesday, July 9, 2014 1:47 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 1:30 AM
Points: 4, 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
Post #1590631
Posted Wednesday, July 9, 2014 6:38 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, April 24, 2015 1:48 PM
Points: 201, Visits: 2,000
The -- operator would cause quite an issue, since that is the comment mark
Post #1590704
Posted Wednesday, July 9, 2014 7:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 1:30 AM
Points: 4, Visits: 17
Indeed. "set @j = @i--" is also a nice joke. Good idea for placing some T-SQL bombs in our companies :)

Post #1590728
Posted Wednesday, July 9, 2014 7:58 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, August 17, 2016 1:06 PM
Points: 1, Visits: 40
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.
Post #1590759
Posted Wednesday, July 9, 2014 8:45 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 7:25 PM
Points: 19,955, Visits: 18,188
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
Post #1590790
Posted Wednesday, July 9, 2014 9:03 AM
SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 1:19 PM
Points: 9,327, Visits: 8,659
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
Post #1590800
Posted Wednesday, July 9, 2014 9:07 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, April 24, 2015 1:48 PM
Points: 201, Visits: 2,000
we could use a like button on this forum... on all the posts that are saying this is an unnecessary extension to t-sql
Post #1590808
Posted Thursday, July 10, 2014 5:42 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 2:26 PM
Points: 1,986, Visits: 6,656
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
Post #1591102
Posted Friday, July 15, 2016 7:09 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, July 27, 2016 5:46 AM
Points: 30, Visits: 15
I was curious how these two concatenate examples compared in speed, I placed the examples in their own tabs in my local Sql Server 2014 and turned on Include Client Side Statistics for each example, then ran them.

The non-shorthand example beat the shorthand in Client Execution Time, Bytes received from server, Total Execution time, and Wait time on server replies.

From that I'm inferring that just because you type less characters, doesn't mean it's the best solution.

Now I'm wondering if I'm seeing the stats correctly, do y'all get those results too?
Post #1802089
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse