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 «««1234»»

Incremental additions Expand / Collapse
Author
Message
Posted Friday, October 29, 2010 8:53 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: Monday, May 21, 2012 3:13 PM
Points: 516, Visits: 1,563
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



Post #1013127
Posted Friday, October 29, 2010 9:39 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, January 24, 2014 10:11 AM
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

Post #1013170
Posted Friday, October 29, 2010 9:45 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 17, 2011 9:34 AM
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.
Post #1013175
Posted Friday, October 29, 2010 10:05 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 5, 2014 2:00 PM
Points: 2,160, Visits: 2,191
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.
Post #1013193
Posted Friday, October 29, 2010 11:39 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, November 24, 2014 8:33 AM
Points: 1,676, Visits: 1,760
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
Post #1013245
Posted Friday, October 29, 2010 12:43 PM


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:24 AM
Points: 3,115, Visits: 995
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.
Post #1013280
Posted Saturday, October 30, 2010 5:52 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 7:11 PM
Points: 7,920, Visits: 9,646
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
Post #1013444
Posted Sunday, October 31, 2010 6:10 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Sunday, November 17, 2013 11:53 AM
Points: 623, Visits: 237
Good Question. and very good follow up threads by some members. it clears some lack of answer explanations.
Post #1013650
Posted Sunday, October 31, 2010 9:46 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 4:18 PM
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.
Post #1013663
Posted Tuesday, November 2, 2010 2:28 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 7:55 AM
Points: 18,060, Visits: 16,090
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
Post #1014890
« Prev Topic | Next Topic »

Add to briefcase «««1234»»

Permissions Expand / Collapse