In-line variable assignment 2

  • Hugo, I thought the reason why the answer is null is because the evaluation of IF 1 < 0 get returned as false

  • Good question, thanks, Hugo.

  • Thank you for the post, Hugo, good one.

    (so simple... declaration takes precedence of value assignment, even though it is in-line)

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • Thanks, all, for the nice words! 😉

    Sektor / Iulian: Yes, this is indeed different from C#, which (as far as I know - probably not very far) uses multiple-pass compilation. T-SQL uses single-pass, and has very different scoping rules. As far as I know, that is indeed by design, though badly documented.

    thokozanik (8/11/2014)


    Hugo, I thought the reason why the answer is null is because the evaluation of IF 1 < 0 get returned as false

    That is part of the explanation. Because 1 < 0 evaluates as false, the (implied) SET statement that gives @j-2 its value is never executed. (And neither is the SET that changes the value of @i, though that becomes irrelevant for the question once you realize why @j-2 is not initialized at all).


    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/

  • Nice little question, and great explanation. Thanks.

  • Explanation accepted but why doesn't this return an error as the declaration for @j-2 is never executed .

    Is it that the sql is evaluated for all the possible declarations before the code is executed ?

    Oops , re-read the answer .. the code is pre-compiled 🙂 got it

  • Nice one, thanks Hugo

    Thanks

  • Tricky.. 🙂 lost my points

Viewing 8 posts - 16 through 22 (of 22 total)

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