Incremental additions

  • Comments posted to this topic are about the item Incremental additions

    [font="Comic Sans MS"]Praveen Goud[/font]

  • Nice question. Thanks

    Thanks

  • Good question.

    Thanks

  • Great question. Thank-you.

    Thanks & Regards,
    Nakul Vachhrajani.
    http://nakulvachhrajani.com

    Follow me on
    Twitter: @sqltwins

  • Good question, but I've liked a little more explanation and if possible, a reference.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Great question, thanks

    Here is a reference to the compound operators: http://msdn.microsoft.com/en-us/library/cc645922.aspx

    Regards,

    Iulian

  • I guess the subtle point is that SQL Server will NOT throw any syntax errors because ultimately what the user is making is a logical error.

    It's one of those things that one would miss when writing code in a hurry, and then spend endless nights trying to figure out what went wrong.

    Thanks & Regards,
    Nakul Vachhrajani.
    http://nakulvachhrajani.com

    Follow me on
    Twitter: @sqltwins

  • Iulian -207023 (10/29/2010)


    Great question, thanks

    Here is a reference to the compound operators: http://msdn.microsoft.com/en-us/library/cc645922.aspx

    Regards,

    Iulian

    Thanks for the reference, but it still doesn't explain the question. In the question =+ is used, while the compound operator used in SQL Server is +=. I've done a little research, and it seems that a++ is the post-increment operator and ++a the pre-increment operator.

    (see http://bytes.com/topic/c/answers/506780-difference-between-pre-increment-post-increment).

    So it seems that the explanation of the question is wrong, as it states that the pre-increment operator is not used in SQL Server. SQL Server uses however a compound operator, meaning addition and assign, not incrementing the value. It still remains unknown to me why SQL Server ignores the + in =+ instead of giving a syntax error.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • So it seems that the explanation of the question is wrong, as it states that the pre-increment operator is not used in SQL Server. SQL Server uses however a compound operator, meaning addition and assign, not incrementing the value. It still remains unknown to me why SQL Server ignores the + in =+ instead of giving a syntax error.

    Yes, explanation is wrong!

    @a=+@b means simply that you assign an +(expression) to @a.

    + is unary operator: http://msdn.microsoft.com/en-us/library/aa276846(SQL.80).aspx

  • Carlo Romagnano (10/29/2010)


    So it seems that the explanation of the question is wrong, as it states that the pre-increment operator is not used in SQL Server. SQL Server uses however a compound operator, meaning addition and assign, not incrementing the value. It still remains unknown to me why SQL Server ignores the + in =+ instead of giving a syntax error.

    Yes, explanation is wrong!

    @a=+@b means simply that you assign an +(expression) to @a.

    + is unary operator: http://msdn.microsoft.com/en-us/library/aa276846(SQL.80).aspx

    Thanks for the link. Hence, @a =+ @b-2 means @b-2 value assign to @a so @a = -21.

    Thanks

  • If the author wanted to trick us with the C-like pre-increment operator, this script would be more funny: 🙂

    ...

    select @a = ++@b

    select @a A

    ...

  • Hardy21 (10/29/2010)


    Thanks for the link. Hence, @a =+ @b-2 means @b-2 value assign to @a so @a = -21.

    Indeed. But why doesn't SQL Server give a syntax error? I would've liked that in the explanation.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • da-zero (10/29/2010)


    Hardy21 (10/29/2010)


    Thanks for the link. Hence, @a =+ @b-2 means @b-2 value assign to @a so @a = -21.

    Indeed. But why doesn't SQL Server give a syntax error? I would've liked that in the explanation.

    Becuase there is no invalid syntax. Spaces are (mostly) ignored by SQL Server, so @a =+ @b-2 is equivalent to @a=+@b, or to @a = +@b. The latter alternative is the most human-comprehensible. @a is assigned the result of applying the unqry + operator on @B. (And since the unary + operator is basically a no-op, you simply assign @a the value of @b-2).


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hugo Kornelis (10/29/2010)


    da-zero (10/29/2010)


    Hardy21 (10/29/2010)


    Thanks for the link. Hence, @a =+ @b-2 means @b-2 value assign to @a so @a = -21.

    Indeed. But why doesn't SQL Server give a syntax error? I would've liked that in the explanation.

    Becuase there is no invalid syntax. Spaces are (mostly) ignored by SQL Server, so @a =+ @b-2 is equivalent to @a=+@b, or to @a = +@b. The latter alternative is the most human-comprehensible. @a is assigned the result of applying the unqry + operator on @B. (And since the unary + operator is basically a no-op, you simply assign @a the value of @b-2).

    Great. Thanks for the explanation! Now it seems really simple. 🙂

    (I was waiting until you would post on this thread and take all doubt away :-D)

    edit: your explanation comfirms my initial statement that the explanation of the questions' poster is incorrect, as it has nothing to do with pre-increment operators, but with misplacing the + so that SQL Server ignores the unary operator. Maybe the question would have been more difficult (or tricky) if it contained =-

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • da-zero (10/29/2010)


    edit: your explanation comfirms my initial statement that the explanation of the questions' poster is incorrect, as it has nothing to do with pre-increment operators, but with misplacing the + so that SQL Server ignores the unary operator. Maybe the question would have been more difficult (or tricky) if it contained =-

    Agreed; the explanation is lacking.

    Pre-increment operators in C++ are a double + (or a double -) before the operand. Like "a = ++b" - this adds 1 to b, then assigns the result to a. You can also use post-increment: "a = b++" first assigns the value of b to a, then adds one to the value of b. And if you use -- instead of ++, you get pre- and post-decrement.

    SQL Server understands neither of these. The post-increment syntax should result in a syntax error. The pre-increment syntax runs, but is interpreted as a sequence of two unary plus operators (so "SET @a = ++@b;" is interpreted as "SET @a = +(+(@b));"). With decrement, the results are reversed - post-decrement runs (because "SET @a = @b--;" is interpreted as "SET @a = @b-2", since -- is the start of an inline comment). The pre-decrement does not run (also becuase -- is a comment token).

    Based on the supplied answer options and the explanation, I think the question author tried to confuse us with the syntax for compound operators, that was introduced in SQL Server 2008. "SET @a += @b-2;" (note that the order of = and + is not the same as in the QotD!) will generate an error message in SQL Server 2005 and older; in SQL Server 2008 and later, it will add the value of @b-2 to @a, i.e., it is equivalent to "SET @a = @a + @b-2;". One of the answers provided matches exactly what would be returned if the query had used += instead of =+.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

Viewing 15 posts - 1 through 15 (of 31 total)

You must be logged in to reply to this topic. Login to reply