• Tom.Thomson (7/27/2010)


    Hugo Kornelis (4/21/2010)


    I never understand that habit. The semicolon is a statement terminator, not a statement starter, so the logical place for it is at the end of a statement, not before the next one.

    The semicolon has always been part of the Transact SQL syntax, but unlike almost every other language, it was optional.

    Can you suggest a language in which it is used as a terminator rather than as a separator? I can't think of one.

    I remember the early C++ compilers (the ones which compiled C++ into C and then called a C compiler to get a binary) which were sensitive to the fact that it was not a terminator, but a separator, and didn't like empty statements. So if you wrote a semicolon at the end of the last statement before the terminating brace of a compound statement the parser threw an error. You also got an error if you wrote a semicolon after the closing brace of a compound statement.

    Is T-SQL going to break with this tradition and allow semicolon immediately before or immediately after END?

    Yes. The semicolon is a terminator in T-SQL, not a seperator. This is valid code:

    IF @Var = 1

    BEGIN;

    PRINT 'It is 1';

    END;

    ELSE

    BEGIN;

    PRINT 'It is not 1';

    END;

    There is no semicolon after IF @Var = 1 or after ELSE because they need a statement (or a BEGIN END block) to be complete.

    There is one annoying exception - in a TRY CATCH block, you get an error when you terminate END TRY with a semicolon. :crying:

    I don't know enough about C++ to comment about the terminator / seperator difference, so I'll just take your word for it.

    And hence, nobody ever used it. This first changed when the SQL Server 2005 parser required a statement to be terminated in order to recognise WITH as the CTE starter, not as a query hint.

    And that was a bad piece of language design. If the parser team were unable conveniently to distinguish the two uses of "WITH" (which suggests either incompetence or unreasonable schedule presure) they should have insisted on a different keyword being used for CTEs, to preserve the language's ability to live without a statement separator.

    Only they could not use anything but WITH for the CTE start, because that is how a CTE is defined in the ANSI standard for SQL. And they could not change the use of WITH for hints either, cause that would break existing code.


    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/