While loop 2

  • Thanks for the question. I was kind of expecting something like this after the first question. Natural follow on to it.

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • demonfox (10/12/2012)


    Lokesh Vij (10/11/2012)


    Thanks Sreenivas for the question. Indeed we discussed the situation of "Divide by zero error" in your earlier QOTD discussion forum "While Loop - 1".

    All in all ending this week on a good note 🙂

    And I didn't have to rethink for this one..;-):w00t::hehe:

    I hope demonfox, you were the one who started that "Divide by Zero" post the other day 🙂

    ~ Lokesh Vij


    Guidelines for quicker answers on T-SQL question[/url]
    Guidelines for answers on Performance questions

    Link to my Blog Post --> www.SQLPathy.com[/url]

    Follow me @Twitter

  • Thanks for the question. I almost answered it wrong 'til I had a second look at it. Thank goodness for morning caffeine. 😀



    Everything is awesome!

  • it's my mistake. i read few lines and gave the answer without reading it completely.

    Anyway good question.:-)

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • I guess the thing that confuses me is, if the variable @ctr is declared within the try block, and the try block fails, why the variable would even exist to be selected in the catch block. Clearly it is, I just don't understand the logic.

    Then again, I'd never declare a variable within a try block anyway, so...

    ron

    -----
    a haiku...

    NULL is not zero
    NULL is not an empty string
    NULL is the unknown

  • Great question!

  • ronmoses (10/12/2012)


    I guess the thing that confuses me is, if the variable @ctr is declared within the try block, and the try block fails, why the variable would even exist to be selected in the catch block. Clearly it is, I just don't understand the logic.

    Then again, I'd never declare a variable within a try block anyway, so...

    ron

    Variables in SQL are not block scoped - you can put variable declarations between begin and end and use the variables after the end.

    I have a vague recollection from ancient times that someone wanted to be purist about begin and end being compound statement brackets for control flow, nothing to do with lexical scope. SQL variables are module (stored procedure, trigger, udf) scoped and batch scoped. As far as I recall, SQL is the only mainstream language that ever did this (but I've forgotten more computer languages than I remember, so I could easily be wrong).

    BoL: Transact-SQL Variables


    The scope of a variable lasts from the point it is declared until the end of the batch or stored procedure in which it is declared.

    Tom

  • Variables in SQL are not block scoped - you can put variable declarations between begin and end and use the variables after the end.

    I have a vague recollection from ancient times that someone wanted to be purist about begin and end being compound statement brackets for control flow, nothing to do with lexical scope. SQL variables are module (stored procedure, trigger, udf) scoped and batch scoped. As far as I recall, SQL is the only mainstream language that ever did this (but I've forgotten more computer languages than I remember, so I could easily be wrong).

    Perl will let you do this if you don't use strict. Otherwise T-SQL is unique in this way, and a bit odd.

    Tony
    ------------------------------------
    Are you suggesting coconuts migrate?

  • I wonder how the 17% can think that a variable of type int can hold a "Error" value. May be null, but error?

    By the way, what is the "Error" value? 😛

  • Saw the division by zero error.... and knew it was 4.

    Thanks for a question that I can unequivically feel that I know the answer to 🙂



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • Nice question. Thanks!

  • Thanks for the question

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Good Question +1

  • Good question. Thanks for submitting.

    http://brittcluff.blogspot.com/

  • As easy and basic as the previous QOTD

Viewing 15 posts - 16 through 29 (of 29 total)

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