CTE Syntax

  • Luis Cazares (3/30/2015)


    Eirikur Eiriksson (3/30/2015)


    ;With due respect, additional and extra semicolons between statements will not cause any problems and CTE statements will run, without the semicolon it will not.

    😎

    I beg to differ.

    IF 'You'='Say so'

    ;WITH CTE AS(SELECT 1 n)

    SELECT *

    FROM CTE

    GO

    CREATE VIEW WillItBreak

    AS

    ;WITH CTE AS(SELECT 1 n)

    SELECT *

    FROM CTE

    GO

    Between statements not within statements, create view is a single statement;-)

    😎

  • Let's be honest requiring statement terminators is to make life easier on the developers writing the syntax parser not because there is any inherent value in a ;

  • Instead of inventing the word "beginnator" as the opposite of terminator, how about "initiator"? It's already an English word. Statement initiator vs. statement terminator--I think that sounds reasonable.

    I've seen some confusion as to what constitutes a statement in T-SQL. For example, I've seen this:

    BEGIN TRY;

    -- try block

    END TRY

    BEGIN CATCH;

    -- catch block

    END CATCH;

    There is no semi-colon after END TRY because the parser doesn't allow it. The semi-colons after BEGIN TRY and BEGIN CATCH are empty statements, not statement terminators. Both BEGIN constructs are opening a scope.

    Note that WITH doesn't require a semi-colon on the previous statement if it's the first statement in its scope.

    CREATE PROC try_test AS

    DECLARE @i INT = 2 -- missing a semi colon

    BEGIN TRY

    WITH x AS (SELECT @i AS y)

    SELECT *

    FROM x;

    END TRY

    BEGIN CATCH

    -- do nothing

    END CATCH;

    GO

    EXEC try_test;

    GO

    DROP PROC try_test;

    Of course, you don't want to rely on the beginning of a scope. That declaration should have a semi-colon terminator. But I'm getting tired of seeing "terminators" on BEGIN constructs.

  • Stephanie Giovannini (3/30/2015)


    Instead of inventing the word "beginnator" as the opposite of terminator, how about "initiator"? It's already an English word. Statement initiator vs. statement terminator--I think that sounds reasonable.

    Just to note that it is used incorrectly.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • +1

  • Koen Verbeeck (3/30/2015)


    Such a bad habit to start a CTE with

    ;WITH

    I hate it 😀

    Nice question though, thanks Steve.

    I agree. I've started to try this:

    ;

    WITH myCTE ...

  • Steve Jones - SSC Editor (3/30/2015)


    Koen Verbeeck (3/30/2015)


    Such a bad habit to start a CTE with

    ;WITH

    I hate it 😀

    Nice question though, thanks Steve.

    I agree. I've started to try this:

    ;

    WITH myCTE ...

    Like THAT'S gonna make a difference! :hehe:

  • It's only to let someone easily cut/paste code and exclude the semi-colon.

    I do try to provide good code samples with semi-colons in them to help get people in the habit.

  • Steve Jones - SSC Editor (3/30/2015)


    It's only to let someone easily cut/paste code and exclude the semi-colon.

    I do try to provide good code samples with semi-colons in them to help get people in the habit.

    Second that, good habits are better tough by given an example rather than ranting on them, the copy/paste/snipped nature of coding makes the Beginni(ng)nator method very convenient but it has nothing what so ever to do with other coding practices.

    😎

  • ZZartin (3/30/2015)


    Let's be honest requiring statement terminators is to make life easier on the developers writing the syntax parser not because there is any inherent value in a ;

    That is not the case. Statement terminators don't only make the developers' work easier, but the query parser's as well. A parser with extra code to handle situations which would be clearer with a terminator will use extra CPU time and delay query results.

    As a very simple example, your statement took a small amount of extra effort on my part to parse properly, because you're missing a terminator in there.

  • sknox (3/30/2015)


    ZZartin (3/30/2015)


    Let's be honest requiring statement terminators is to make life easier on the developers writing the syntax parser not because there is any inherent value in a ;

    That is not the case. Statement terminators don't only make the developers' work easier, but the query parser's as well. A parser with extra code to handle situations which would be clearer with a terminator will use extra CPU time and delay query results.

    As a very simple example, your statement took a small amount of extra effort on my part to parse properly, because you're missing a terminator in there.

    I believe it's been demonstrated (more than 50 years ago) that if two languages are identical except that one uses statement separators and the other uses statement terminators, and they allow compound statements, the one using separators costs less to parse. But if they don't have complex statements the parsing complexity of the two is identical (since they are syntactically identical, differing only in the terminology used to describe them). So the terminator case is never easier to parse.

    Tom

  • Stephanie Giovannini (3/30/2015)


    Instead of inventing the word "beginnator" as the opposite of terminator, how about "initiator"?

    In fact you need to use 'beginninginator' if you want to have a chance of taking over the entire tri-state area.:satisfied:

  • sipas (3/31/2015)


    Stephanie Giovannini (3/30/2015)


    Instead of inventing the word "beginnator" as the opposite of terminator, how about "initiator"?

    In fact you need to use 'beginninginator' if you want to have a chance of taking over the entire tri-state area.:satisfied:

    Only if your nemesis is a duck-billed, egg-laying mammal of action. 😛

  • Ed Wagner (3/31/2015)


    sipas (3/31/2015)


    Stephanie Giovannini (3/30/2015)


    Instead of inventing the word "beginnator" as the opposite of terminator, how about "initiator"?

    In fact you need to use 'beginninginator' if you want to have a chance of taking over the entire tri-state area.:satisfied:

    Only if your nemesis is a duck-billed, egg-laying mammal of action. 😛

    Given that the phrase "tri-state area" generally applies to some area of the USA surely a nemesis in that connection would be a member of the Blarina family rather than of Ornithorhyncus? 😀

    Tom

  • TomThomson (3/31/2015)


    Ed Wagner (3/31/2015)


    sipas (3/31/2015)


    Stephanie Giovannini (3/30/2015)


    Instead of inventing the word "beginnator" as the opposite of terminator, how about "initiator"?

    In fact you need to use 'beginninginator' if you want to have a chance of taking over the entire tri-state area.:satisfied:

    Only if your nemesis is a duck-billed, egg-laying mammal of action. 😛

    Given that the phrase "tri-state area" generally applies to some area of the USA surely a nemesis in that connection would be a member of the Blarina family rather than of Ornithorhyncus? 😀

    But 'Perry the American short-tailed shrew' is not as snappy.:-)

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

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