SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Starting a CTE


Starting a CTE

Author
Message
Steve Jones
Steve Jones
SSC Guru
SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)

Group: Administrators
Points: 563775 Visits: 20837
Comments posted to this topic are about the item Starting a CTE

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Bob Razumich
Bob Razumich
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1608 Visits: 1325
I am so looking forward to the arguments about where the semicolon should go exactly.
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSC Guru
SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)

Group: General Forum Members
Points: 57055 Visits: 7966
Nice easy one, thanks Steve.

Given that I have been coding in C# quite a bit lately, it has become my habit to place the semi-colon at the end of each statement....

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
RoNoS
RoNoS
SSC Eights!
SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)SSC Eights! (837 reputation)

Group: General Forum Members
Points: 837 Visits: 328
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.)
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)

Group: General Forum Members
Points: 222801 Visits: 13367
If you need to resort to this, you have a very bad coding practice.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Shayn Thomas
Shayn Thomas
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2696 Visits: 582
Nice easy one, thanks Steve!
Steve Jones
Steve Jones
SSC Guru
SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)SSC Guru (563K reputation)

Group: Administrators
Points: 563775 Visits: 20837
Koen Verbeeck - Thursday, July 12, 2018 1:24 AM
If you need to resort to this, you have a very bad coding practice.


Or you deal with legacy code.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Sean Lange
Sean Lange
SSC Guru
SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)

Group: General Forum Members
Points: 241889 Visits: 19192
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)

Group: General Forum Members
Points: 241889 Visits: 19192
Was helping somebody just yesterday on another site and they couldn't for the life of them figure out why their view was throwing a syntax error. The view started with a cte that had a leading semicolon. They claimed they had been wrestling the thing for a couple of days. I used Lynn's terminology that the semicolon is not a beginninator, but a terminator. And once they removed the semicolon their view was just fine. I don't know why the coding style of starting a cte became to be but it causes so much confusion.

_______________________________________________________________

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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
lmalatesta
lmalatesta
SSC-Enthusiastic
SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)SSC-Enthusiastic (188 reputation)

Group: General Forum Members
Points: 188 Visits: 118
Koen Verbeeck - Thursday, July 12, 2018 1:24 AM
If you need to resort to this, you have a very bad coding practice.

Testify!

One of my largest complaints about T-SQL is that Microsoft decided that the statement terminator operator would be optional.

Officially not using semicolons to terminate every T-SQL statement is has been deprecated since the release of SQL Server 2008, "Although the semicolon is not required for most statements in this version of SQL Server, it will be required in a future version."

The smart money is on Microsoft never making statement terminators mandatory. The amount of T-SQL code that would break is mind boggling.

Nevertheless, everyone coding T-SQL should be using semicolons to terminate every statement.

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search