CTE Syntax

  • Comments posted to this topic are about the item CTE Syntax

  • Thanks Steve for the nice question..as usual.

    I explored more on the cause.

    β€’You must remember to terminate the statement preceding the CTE with a semicolon, otherwise SQL will throw this error message at you:

    Incorrect syntax near the keyword β€˜with’. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

    Thanks.

  • Thanks for the question Steve!

    Now I can see yet another discussion on Terminators vs. Beginingnators coming, must say I'm with the latter as my muscle memory punches a semicolon whenever I'm about to write ;WITH:-D

    😎

  • Good question. Also it's a general recommendation to use semi-colons for all statements in t-sql codes. People usually do not always. Thanks.

    Igor Micev,My blog: www.igormicev.com

  • Such a bad habit to start a CTE with

    ;WITH

    I hate it πŸ˜€

    Nice question though, thanks Steve.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Koen Verbeeck (3/30/2015)


    Such a bad habit to start a CTE with

    ;WITH

    I hate it πŸ˜€

    .This .It's a statement terminator .It has no place at the beginning of statements .I can't imagine anyone who writes front-end languages (C#, Java, C++, etc) ever even considering starting a statement with a terminator ?So why are we being so difficult about it and writing code that suggests to someone reading later that we don't even know the basic principals of programing languages

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Koen Verbeeck (3/30/2015)


    Such a bad habit to start a CTE with

    ;WITH

    I hate it πŸ˜€

    Nice question though, thanks Steve.

    +1 - maybe part of the answer should be promoting the correct way to write code, rather than the nasty workaround. Good question though.

  • This was removed by the editor as SPAM

  • You could find a possible explanation in this link

    As I am registered for CodeProject , I have already read this article and I got easily the point.

    I have also heard a person who explained that he was always beginning his code with a ; to avoid any conflict with a previous statement in case of a copy/paste of T-SQL code ( I am applying this rule which seems to a good one )

  • patricklambin (3/30/2015)


    You could find a possible explanation in this link

    As I am registered for CodeProject , I have already read this article and I got easily the point.

    I have also heard a person who explained that he was always beginning his code with a ; to avoid any conflict with a previous statement in case of a copy/paste of T-SQL code ( I am applying this rule which seems to a good one )

    Why don't you start your sentences/documents/emails with a . as well?

    Just in case someone copy pastes your text πŸ˜‰

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • The correct answer is (e) because questions like this promote sloppy coding

  • ;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.

    😎

  • .But starting statements with terminators teaches bad habits .For example I had a dev recently who scattered ; randomly until the SQL proc started working .Once it had been explained that the ; is a terminator just like in C# he put them correctly and asked me "Then why do people put them at the beginning of statements? That's just stupid." (.His words) .He'd read up on CTEs online and concluded ,based on some people putting the ; at the end and others at the beginning ,that it wasn't a statement terminator ,that it was just some command that no one explained what it did and that followed rules for placing that no one explained

    ?How about we just teach people correctly instead

    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.

    😎

    .Without ; C# code won't work .We don't see samples of C# code starting with statement terminators

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • .dup

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 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.

    😎

    .Yes that's true

    ;but it doesn't make it a good way to code

    .Surely we should be promoting good practice

    , rather than something random that happens to work

    ?

Viewing 15 posts - 1 through 15 (of 64 total)

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