• RoNoS - Thursday, July 12, 2018 1:13 AM

    Any code before the "WITH STATEMENT" must end with a terminator, which is the semi colon. This prevents errors if this code is placed after other statements in a batch.
    As in: Not each CTE needs a semicolon. You only need one. (And it is optional when it is the first statement in the batch.)

    Actually the semicolon does not even belong to the cte, it is terminating the previous statement. A CTE requires that the previous statement is terminated with a semicolon. It is very common for people to put them at the beginning to ensure the previous statement was terminated.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/