EXECUTE

  • Hugo Kornelis (2/14/2012)


    Koen Verbeeck (2/14/2012)


    kll 51891 (2/14/2012)


    I would have expected it to fail due to no newline or semicolon.

    But it were not so

    Anywhere I can find specific rules about when to use and when not to use semicolon?

    Except the reserved word ";with" of course.

    The semicolon is not yet obligated, except when using the WITH clause. This probably will change in a future version.

    If there's a statement before the WITH clause, it should be terminated with a semicolon. Pay attention, this is not the same as saying that it should be ";WITH". If everyone starts terminating statements with semicolon in old code to make the code portable to a newer edition of SQL Server, all ;WITH statements will fail.

    Most of the above is true.

    Terminiating SQL statements with a semicolon has always been allowed in SQL Server, but it was optional. WITH was the first keyword (but is not the only one) that requires the statement before it (if any) to be semicolon-terminated, otherwise the parser would think that the WITH keyword was for query hints. It has already been announced that in some future version, omitting the semicolon will be disallowed in all cases. There has not been any mention as to which version that will be. But it's a good idea to start getting into the habit now of always using the semicolon statement terminator.

    I consider the use of ;WITH a bad habit, as it works around one limitation while not addressing the true underlying issue. And it will come back to bite you when terminating is no longer optional. However, it does currently work - not because he parser accepts prepending WITH with a semicolon as a viable alternative, but because the parser ignores whitespace and line breaks - so it "sees" the semicolon right after the end of the preceding statement and interprets it as a statement terminator.

    I also consider ;WITH a bad habit because it already breaks things. If you use WITH a lot, and get in the habit of prepending the semicolon, chances are you'll end up writing code like the following:

    CREATE VIEW v_sample AS

    ;WITH a AS ...

    which will not execute because the semicolon terminates the CREATE VIEW statement before it's complete. Sure, that's easy to fix, but if you're in the habit of prepending the semicolon, you'll have to fix it every time. If, on the other hand, you get in the habit of terminating statements with the semicolon instead, you won't have anything to fix.

  • John Mitchell-245523 (2/14/2012)


    Appalled? I usually reserve that reaction for earthquakes, famines, wars and so on.

    In any case, I don't agree that just because two statements are posted on the same page without a GO between them, we should necessarily assume that they are to be executed as a single batch. OK, so the question could have said "assuming that they are executed separately", but if we start down that road, we'd end up with a list of terms and conditions longer than the question itself. In my opinion, the intent of this question was clear.

    John

    +1

  • John Mitchell-245523 (2/14/2012)


    Appalled? I usually reserve that reaction for earthquakes, famines, wars and so on.

    In any case, I don't agree that just because two statements are posted on the same page without a GO between them, we should necessarily assume that they are to be executed as a single batch. OK, so the question could have said "assuming that they are executed separately", but if we start down that road, we'd end up with a list of terms and conditions longer than the question itself. In my opinion, the intent of this question was clear.

    John

    +1

    Also, the Merge statement is another one that requires a semicolon at the end.

  • tks for the question

  • Obligatory pedantic complaint: they're parentheses, not brackets. These are brackets: []. (For completeness, these are braces: {}.)

    Noted; thank you.

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle

  • sknox (2/14/2012)

    Obligatory pedantic complaint: they're parentheses, not brackets. These are brackets: []. (For completeness, these are braces: {}.)

    Where I come from, () are brackets, [] are square brackets, and {} are curly brackets 🙂

  • Good question.

  • Koen Verbeeck (2/14/2012)


    The semicolon is not yet obligated, except when using the WITH clause.

    MERGE (the statement, not the join hint!) is required to be terminated with a semicolon.

  • Good Question... Thank you...

  • One question though if you execute you should get error below,

    Msg 134, Level 15, State 1, Line 9

    The variable name '@sqlstring' has already been declared. Variable names must be unique within a query batch or stored procedure.

    once you fix the above may be your answer would right 🙂

    Cheer Satish 🙂

  • One question though if you execute you should get error below,

    Msg 134, Level 15, State 1, Line 9

    The variable name '@sqlstring' has already been declared. Variable names must be unique within a query batch or stored procedure.

    actually not. 2 statements, not a single batch query.

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle

  • Good question.

  • While we're being pedantic...

    I would have thought the answer depends on your permissions. If you can't run dbcc showfilestats then both will fail.

    ...One of the symptoms of an approaching nervous breakdown is the belief that ones work is terribly important.... Bertrand Russell

  • Henrico Bekker (2/16/2012)


    actually not. 2 statements, not a single batch query.

    There aren't two statements, there are 6. If you'd said "two batches of statements" instead of "two statements" the thing would have made sense. But you didn't. Since it is necessary to execute at least three statements - ie a batch of statements not a single statement - to get any meningful result, we have to assume you are referring to two of the statements in however many batches you supplied; and you supplied something that was plainly and clearly one batch, not two, so it was reasonable to assume it was intended to as a singe batch (Occam's razor is applicable: there was no "go" anywhere between the two declarations, and there was no necessity to invent one). So as it stands, your reply to Satish seems to me to be nothing other than inaccurate nonsense.

    Are think you are lucky there's no-one screaming about a "trick" question; I'm not, because I never attribute to malice (nor even to stupidity) that which is more easily explained as a simple oversight. Satish didn't eithesuggest a "trick" either, maybe for the same reason.

    And, just as an aside: I now see that I must use more than 2 emoticons per post if I want to clown anywhere John Mitchell-245523 is likely to see it.

    Tom

  • So as it stands, your reply to Satish seems to me to be nothing other than inaccurate nonsense.

    next time, I'll run it past you, to ensure it meets YOUR requirements.

    I'll ask Steve to give you the 1 point as it ruined your day apparently.

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle

Viewing 15 posts - 31 through 45 (of 49 total)

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