Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

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: Friday, June 6, 2014 2:06 PM
Points: 1,040, Visits: 277
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

Need SQL Server Examples check out my website at http://www.sqlserverexamples.com
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: Today @ 5:20 AM
Points: 2, Visits: 12
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-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 5:59 AM
Points: 141, Visits: 1,736
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: Today @ 5:20 AM
Points: 2, Visits: 12
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, July 9, 2014 9:23 AM
Points: 1, Visits: 17
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:37 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, July 20, 2014 11:55 AM
Points: 1,945, Visits: 2,860
These shorthands are a bad idea. You gain no advantages and destroy portability of your code. If this were a writing forum, who would encourage people to write English like a hillbilly? This was put into T-SQL to tie you to MS and make non-SQL, C-family programmers feel better.

Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1590788
Posted Wednesday, July 9, 2014 8:45 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 2:58 PM
Points: 21,223, Visits: 14,928
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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1590790
Posted Wednesday, July 9, 2014 9:03 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 12:41 PM
Points: 3,960, Visits: 2,970
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-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 5:59 AM
Points: 141, Visits: 1,736
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


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 4:49 AM
Points: 556, Visits: 2,581
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



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)
My blog
Post #1591102
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse