Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Incremental additions


Incremental additions

Author
Message
steve block
steve block
Mr or Mrs. 500
Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)

Group: General Forum Members
Points: 520 Visits: 1563
I hate to admit it, but I got distracted by the fact that the select contained "@a A". Thinking that this was too easy of a question made me guess that the A was some obscure way of applying an absolute value function, hence I was the one person that chose "0,21" for an answer. I hope it comes from infrequent selection of variables to display them and not from turning 60 this year.

Steve



LarrySql
LarrySql
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 116
Yes, I agree. The explanation is incorrect.

To further illustrate what is happening here, try substituting =+ with =-

My interpretation is that you are applying a positive modifier to a negative value,
which obviously results in a negative return. If you apply a negative modifier to a negative number, the result is positive.

 select @a =- @b 



will return +21 when b is -21

;-)
dgarner
dgarner
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 23
Correct me if I'm wrong. It's the spacing that is throwing most people off.

@a =+ @b is the same as @a = (+1) * @b

if i were to write this in my code I would format it as

@a = +@b

but i wouldn't do that since +1 times any number yields that same number

Note that they mention a pre-increment operator but they mean a Compound Operator. Also note Compound Operators are only availabe in SQL 2008. Writing @a += @b would throw a syntex error in earlier version.
UMG Developer
UMG Developer
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2280 Visits: 2204
I too wonder if the intent was to make you think it was the compound "+=" operator that was added in SQL Server 2008. Then the results would be 0, -21.

But since there was no mention of SQL Server 2008 I assumed it wasn't.
Oleg Netchaev
Oleg Netchaev
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1693 Visits: 1808
Oh well, I lost a precious point on this one. The statement in question

select @a =+ @b

looked so ridiculous that it made me think that there were 2 possibilities:

- either it was the author's intention to actually write it as is, perhaps to show how difficult it can be to debug the errors based on misplacing the order of typed characters

- it was a simple typo, and the += was intended

Unfortunately, I chose the second option and thus answered the question incorrectly. I don't consider it a bad question, but cannot say that I learned something from it either, because in reality, nobody can actually benefit from ever typing something like

select @a =+ @b

when select @a = @b does the trick.

Oleg
Rune Bivrin
Rune Bivrin
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3727 Visits: 1462
Oleg Netchaev (10/29/2010)
Oh well, I lost a precious point on this one. The statement in question

select @a =+ @b

looked so ridiculous that it made me think that there were 2 possibilities:

- either it was the author's intention to actually write it as is, perhaps to show how difficult it can be to debug the errors based on misplacing the order of typed characters

- it was a simple typo, and the += was intended

Unfortunately, I chose the second option and thus answered the question incorrectly. I don't consider it a bad question, but cannot say that I learned something from it either, because in reality, nobody can actually benefit from ever typing something like

select @a =+ @b

when select @a = @b does the trick.

Oleg

No, but you can benefit from knowing that it's an easy to make mistake, and that SQL Server won't alert you to the problem.


Just because you're right doesn't mean everybody else is wrong.
TomThomson
TomThomson
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: 10714 Visits: 12017
Not only is the explanation wrong, as several people have pointed out, it is also uses somewhat outdated terminology and is maybe confused about which version of SQL it refers to. The term "positive operator" was used in BoL for SQL 2000, but from SQL 2005 on that term is not used , the operator is called "unary plus" (presumably because MS relised that the term "positive operator" would be likely to confuse people when applied to a copy or no-op operator); but the question is clearly trying to trap people into mistaking =+ for += (why else would the first answer option be exactly what would be seen if the query had had += instead of =+), and += first appeared in SQL 2008, not in SQL 2000.

Tom

Dhruvesh Shah
Dhruvesh Shah
SSChasing Mays
SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)SSChasing Mays (635 reputation)

Group: General Forum Members
Points: 635 Visits: 237
Good Question. and very good follow up threads by some members. it clears some lack of answer explanations.
neil_mccoy
neil_mccoy
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 18
da-zero (10/29/2010)
[quote]Iulian -207023 (10/29/2010)
It still remains unknown to me why SQL Server ignores the + in =+ instead of giving a syntax error.

If I understand correctly, it is not actually a syntax error. As someone else stated, the plus is a unary operator: @a = (+@b). To put in perspective, if the statement was @a =- @b, then @a now becomes 21, essentially @a = - (-21).

Hope that helps.

EDIT: Sorry, didn't realise there were more posts that have already explained this. Ignore at your own will.
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21075 Visits: 18259
Nice question



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

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