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 (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)

Group: Administrators
Points: 620886 Visits: 21265
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.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1746 Visits: 1357
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 (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63531 Visits: 8218
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”
Ron
Ron
SSC Eights!
SSC Eights! (904 reputation)SSC Eights! (904 reputation)SSC Eights! (904 reputation)SSC Eights! (904 reputation)SSC Eights! (904 reputation)SSC Eights! (904 reputation)SSC Eights! (904 reputation)SSC Eights! (904 reputation)

Group: General Forum Members
Points: 904 Visits: 350
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 (238K reputation)SSC Guru (238K reputation)SSC Guru (238K reputation)SSC Guru (238K reputation)SSC Guru (238K reputation)SSC Guru (238K reputation)SSC Guru (238K reputation)SSC Guru (238K reputation)

Group: General Forum Members
Points: 238874 Visits: 13381
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
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4298 Visits: 825
Nice easy one, thanks Steve!

---------------------------------------------------------------------------------------
The more you know, the more you know that you dont know
Steve Jones
Steve Jones
SSC Guru
SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)SSC Guru (620K reputation)

Group: Administrators
Points: 620886 Visits: 21265
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 (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)

Group: General Forum Members
Points: 259988 Visits: 19625
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 (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)SSC Guru (259K reputation)

Group: General Forum Members
Points: 259988 Visits: 19625
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-Addicted
SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)SSC-Addicted (464 reputation)

Group: General Forum Members
Points: 464 Visits: 168
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